This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 2: Set

Overlapping lyrics

face Josiah Wang

Let us practise applying sets to a practical problem!

Since we are on the subject of Frozen, I would like you to write a program to find out whether there are words that Elsa use in common in both the songs “Let It Go” and “Into The Unknown”.

Download the (shortened) lyrics for let_it_go.txt and into_the_unknown.txt, and save them in the same directory as the code you will be implementing.

Now, write a function get_overlapping_words() that takes two strings as its arguments: filename1 and filename2. The function should return a set of words that are common in both files. Assume that each line in the files contains a sentence with words separated by spaces.

I found that Elsa repeats 15 words across both songs (at least in my shortened and edited version of the lyrics).

Sample usage

>>> overlap = get_overlapping_words("let_it_go.txt", "into_the_unknown.txt")
>>> print(overlap)
{'and', 'of', 'a', 'in', 'the', "i've", 'i', "don't", 'is', 'not', 'what', 'go', 'you', 'away', "i'm"}
>>> print(len(overlap))
15