Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How to solve the second Python code? Thank you. 2. Building a Basic Playlist (12 points) Complete the buildPlayList function, which takes three arguments, in

image text in transcribed
image text in transcribed
image text in transcribedHow to solve the second Python code? Thank you.
2. Building a Basic Playlist (12 points) Complete the buildPlayList function, which takes three arguments, in the following order: (a) a dictionary of songs grouped by musical genre (like the one produced by the buildLibrary () function from the previous step) (b) a list of strings representing one or more musical genres (c) an integer n representing the total number of songs to select for the resulting playlist This function returns a list of exactly n 2-element tuples (each tuple consists of a song title, followed by the artist name), randomly drawn from the genres specified above (and ONLY those genres). For example, if the second argument was the three-element list |"hip-hop" "classical, "metal", the new list should only contain songs from those three genres (i.e, the result should not contain any songs that belong to a different genre like "country" or "reggae") . If one or more of the requested genres do not exist in the library, your function should ignore that genre (for example, if the library had no "classical" songs in the example above, then the function should only consider songs from the "hip-hop" and "metal" genres). If the library does not contain ANY of the requested genres, then the function should return an empty list If n is greater than the total number of songs that are available in all of the requested (and available) genres combined, the function should return a list with all of the songs in those genres combined, in any order. For example, if the user requests a 20-song playlist drawn from four genres that have 2, 5 4, and 6 songs respectively, the function should simply return a list with all 17 available songs. Start by constructing a list of potential songs from each of the specified genres. Then use a function like random. sample (from the random module -see the slides for an example of how to use this function) to construct your playlist. import string, randonm def buildLibrary (filename): libraryt for line in open (filename): song-genre, song-title, artist, song-length= line . strip ( )split(',') m, s = song-length . split(':') new lengthint (m) 60 int (s) song_ message (song title, artist, new length) if not song genre in library: library [song genre] [songmessage] - - else: librarylsong_genre].append (song message) # ADD YOUR CODE HERE return library # CHANGE OR REPLACE THIS LINE def buildPlayList (library, genres, length): # ADD YOUR CODE HERE return [] # CHANGE OR REPLACE THIS LINE Compressed Folder ToolsCSE 101 HW 3-1 playlist View Extract s PC DATA (D) Fall 2018 MatenialsSE101.01 CSE 101 HW3-1 playlist Test DocumentSHPasswordSce Text Docurmnt Test DecumentKo Search CSE 101 HW 3-1 playlist Ratio 32% 29% 30% 35% Name ype Date modified library library2 library library4 playlist playlist ntKB N SP1KB No 1 KB 1KB 1KB 1KB 1/30/2018 3-51 PM 1/30/2018 3:51 PM 1/30/2018 3:51 PM 11/30/2018 3:51 PM 1/8/2018 3 47 AM 1/30/2018 5 06PM Text Docum PPS1KB No 1 KB No PDF File Python File 39KB 3% 9 Python FleKSN 3KB 68%

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

Step: 3

blur-text-image

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

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago