Question
Life hack: if you're really bad at karaoke and can't remember the words, you can just repeatedly sing one word. If it's the most common
Life hack: if you're really bad at karaoke and can't remember the words, you can just repeatedly sing one word. If it's the most common word in the song, you'll be right more often than you might think (and may get away with it!).
Write a function approximate_song(filename) that reads the lyrics of the song in the file of name filename, and returns the most common word in the song. In the event of a tie, your function should return the word that comes first alphabetically. Assume that words are whitespace-delimited, and use split with no stripping of punctuation or folding of case to extract the words from the text.
We have provided lyrics for three songs for you to test your function: somebody.txt, barbrastreisand.txt, and fakesong.txt. Note these are not the only files we will use to test your code. You can see the contents of these files by clicking on the tabs at the top-right of the page. Outputs should be as below:
>>> print(approximate_song('somebody.txt'))
that
>>> print(approximate_song('fakesong.txt'))
dum
>>> print(approximate_song('barbrastreisand.txt'))
whooo-oo
MY Incomplete code:
def approximate_song(filename): filename = open(filename, 'r') text = filename.read() filename.close() word_list = text.lower().split(None) word_freq = {} for word in word_list: word_freq[word] = word_freq.get(word, 0) + 1
items = [] for key, value in word_freq.items(): items.append((value, key)) for value, key in sorted(items, reverse=True): return(key)
Please correct my codes, using my incomplete codes. and tell me where did i did wrongly. Thanks!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started