Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions

Question

Compiled languages tend to have later binding times. true or false

Answered: 1 week ago

Question

9. Understand the phenomenon of code switching and interlanguage.

Answered: 1 week ago

Question

2. Why has the conflict escalated?

Answered: 1 week ago