Download the ICStunes program (http://www.ics.uci.edu/~harris/python/ICStunes0.p
ID: 666782 • Letter: D
Question
Download the ICStunes program (http://www.ics.uci.edu/~harris/python/ICStunes0.py) on the lab machine (or whatever machine you and your partner are using) and run it to make sure it works. As you work on this part, make your changes in (copies of) the ICStunes.py file you downloaded; use your lab8.py file for the other parts of this assignment.
(6) Generalize the top_n_played function (i.e, make it apply to a broader range of criteria than just play counts) as follows: Write a function called top_n that takes a list of albums and a number, as before, plus two additional parameters—(the name of) a function we can use as a sort key for comparing albums and a Boolean (that's true if you want the n highest values and false if you want the n lowest). Thus, you could use the new top_n to produce the same result as top_n_played by calling
top_n(MUSIC, 3, play_count_from_songdisplay, True)
and you could use it to produce the 10 shortest songs by calling
top_n(MUSIC, 10, length_from_songdisplay, False)
(7) Total listening time isn't the only way of determining a favorite album. Generalize your favorite_album function by writing a function called favorite_album2 that takes a list of albums and a second argument—a "favorite measurement function" that favorite_album2 can apply to each album, comparing those results to determine the favorite. This call to favorite_album2 would behave the same way as a call to the original favorite_album function:
favorite_album2(MUSIC, Album_listening_time)
Write at least one example of a favorite measurement function other than total listening time. Then test your favorite_album2 function by applying that new function. [Hint: Songdisplays aren't involved in this part.]
(8) Music manager programs typically provide a search box into which you can type a keyword; the program then searches your collection for songs containing that keyword in their title, their artist, or their album's title. Write a function called collection_search that behaves in the same way, taking a collection and a string as parameters and returning a list of Songdisplays of songs whose title, artist, or album title include that string.
Explanation / Answer
6. The required function is as below,
def top_n(MC: 'list of Album', n: int, sortFunc,isHighest:bool) -> 'list of Songdisplay':
''' Return the top n most frequently played songs in MC
'''
list_of_Songdisplays = all_Songdisplays(MC)
list_of_Songdisplays.sort(key=sortFunc,reverse=isHighest)
return list_of_Songdisplays[:n]
7.Here, a function is being written to find all the favorite album having release year is moreb than 1980.
Below is the code,
def favorite_album2(MC:'list of Album', isFavourite:'favorite measurement function'):
fav = []
for album in MC:
if(isFavourite(album)):
fav.append(album)
return fav
def isReleasedAfter1980(album:Album):
if(album.year > 1980):
return True
return False
8.Below, is the required function.It iterates over album and trying to find the song fullfilling the creterion.
def collection_search(MC,searchableKey):
searchedSongs = []
for album in MC:
if((searchableKey in album.title) or (searchableKey in album.artist)):
for s in album.songs:
searchedSongs.append(s)
else:
for s in album.songs:
if(searchableKey in s.title):
searchedSongs.append(s)
return searchedSongs
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.