Question

COMPLETE THE FOLLOWING QUESTION def find_favourite_artist(filename: str) -> str: ''' Return the name of the artist...

COMPLETE THE FOLLOWING QUESTION
def find_favourite_artist(filename: str) -> str:
    '''
    Return the name of the artist that occurs most frequently in the song file
    of the given filename. If there are more than one artists that occur most
    frequently, just return any of them.

    Note: Feel free to create helper functions for this if you need to.

    Hint: You may want to use the "sorted" function.
    '''

    pass
THE SONG FILE IS:
Title, Singer, Release Year
Let it go, Idina Menzel, 2013
Happy,  Pharrell Williams, 2013
Airplanes,  B.o.B, 2010
Eyes Open, Taylor Swift, 2012
Stay with me, Sam Smith, 2014
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

def read_file_and_return_artist_list(fileName):
try:
with open(fileName) as f:
content = f.readlines()
content = [x.strip() for x in content]
singersList=[]
for record in content :
records= record.split(",");
singersList.append(records[1])
return singersList[1:] #ignore first as that is header
except:
print("Error in opening file")
return []
def most_frequent(inList):
counts = dict();
for singersList in inList:
if singersList in counts:
counts[singersList] +=1;
else:
counts[singersList] = 1;
maxCount = 0;
for record in counts :
if(maxCount<counts[record]):
maxCount = counts[record];
nlst =[]
for record in counts :
if(maxCount==counts[record]):
nlst.append(record);
return nlst;


file = input("Please enter file name : ")
singersList=read_file_and_return_artist_list(file)
mostFreq=most_frequent(singersList)
print("Most frequent singer")
for singer in mostFreq:
print(singer)

singer.txt

Title, Singer, Release Year
Let it go, Idina Menzel, 2013
Happy, Pharrell Williams, 2013
Airplanes, B.o.B, 2010
Eyes Open, Taylor Swift, 2012
Stay with me, Sam Smith, 2014
Airplanes, B.o.B, 2010
Airplanes, B.o.B, 2010
Airplanes, B.o.B, 2010
Airplanes, B.o.B, 2010
Airplanes, B.o.B, 2010
Airplanes, B.o.B, 2010
Airplanes, B.o.B, 2010
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Airplanes, B.o.B, 2010
Airplanes, B.o.B, 2010
Airplanes, B.o.B, 2010
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012
Eyes Open, Taylor Swift, 2012

output:

Add a comment
Know the answer?
Add Answer to:
COMPLETE THE FOLLOWING QUESTION def find_favourite_artist(filename: str) -> str: ''' Return the name of the artist...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT