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
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:
COMPLETE THE FOLLOWING QUESTION def find_favourite_artist(filename: str) -> str: ''' Return the name of the artist...
def contains_song(title: str, filename: str) -> bool: ''' Return True iff title (the name of a song as a string) is included in the file with of the given filename. '''