Make sure all programs are saved to your cs21/labs/08
directory. Files outside this directory will not be graded.
$ update21
$ cd ~/cs21/labs/08
The first part of the lab involves you tracing linear and binary searches on a list. This is excellent practice for what will almost certainly be a question on quizzes and the final exam!
Download the PDF, print it out, and turn it in at the start of class on Friday.
This week’s lab involves processing a music library. Your program music_info.py
will help the user search through their music library and learn interesting information. We strongly recommend reading through the entire lab and creating a TDD before implementing your program.
The files below contain music libraries for several different users. There is also a small file music-small.txt
in your lab 8 directory for testing.
/home/smathieson/public/cs21/musicA.txt
/home/smathieson/public/cs21/musicB.txt
/home/smathieson/public/cs21/musicC.txt
Here are the first few lines of musicA.txt
:
100 Years,Five for Fighting,100 Years - Single,244,25,4/10/16; 2:52 PM
11:11 P.M,All-American Rejects,Move Along,184,1,10/21/09; 10:55 AM
1234-007,DDR,DDR,89,2,4/11/14; 2:59 PM
16 Military Wives,The Decemberists,Picaresque,292,20,7/24/14; 2:21 PM
19-2000,Gorillaz,,207,47,7/29/14; 9:00 PM
Each line contains the information about a song. The lines in the file are in alphabetical order based on the name of the song.
Here is a table for what the different data entries mean: song name, artist, album, time (sec), play count, last played
Note that the length of the song is in seconds and the last played shows the date and time.
You will write a program (music_info.py
) that reads in the music data and allows the user to explore this dataset, to see if certain songs are present and how many times they have been played. You will implement a text-based menu system that allows the user to search through the data for either the name of the song (exact match), or any song with a play count in a user-chosen range (e.g., played 40-45 times). When the program quits, it should list all the songs with lengths above 8 minutes (very long songs!)
music_info.py
Here is an example of the program (another, longer example is at the bottom of the page). More detailed requirements are listed after the example, but at a high level you must:
$ python3 music_info.py
========== Welcome to Music Information =========
Enter A,B,C for music library: A
=================================================
(1) Song name (2) Play count (3) Quit : 1
Enter song name: Schoolin' Life
Song name: Schoolin' Life
Artist: Beyoncé
Album: 4 (Deluxe Edition)
Length: 4:53
Play count: 30
Last played: 8/18/15; 4:25 PM
=================================================
(1) Song name (2) Play count (3) Quit : 1
Enter song name: Happy Birthday
Song not found!
=================================================
(1) Song name (2) Play count (3) Quit : 1
Enter song name: london calling
Song name: London Calling
Artist: The Clash
Album: Billy Elliot
Length: 3:20
Play count: 68
Last played: 8/18/17; 3:43 PM
=================================================
(1) Song name (2) Play count (3) Quit : 2
Enter min play count: 70
Enter max play count: 75
Song Play count
------ ------
A Message 73
American Girl 71
Heart of Courage 71
Last Night 72
=================================================
(1) Song name (2) Play count (3) Quit : 2
Enter min play count: 22
Enter max play count: 25
Song Play count
------ ------
100 Years 25
Anyone Else But You 23
Boys Wanna Be Her 24
Cast No Shadow 23
Cliffs of Dover 24
Cosmo Girl 25
Diamond Dogs 22
Fallen Angel 22
Feel Good Inc 24
Feliz Navidad 25
Green Eyes 23
Hands Down 22
Here (In Your Arms) 24
I Believe 23
I Want You 25
I Will Wait 22
Inta Eyh 22
Into the Airwaves 24
La La Lie 24
Last Straw 25
Left Behind 23
Let It Rain 22
Livin' La Vida Loca 25
Made For Each Other 22
Maybe; This Time 23
Miss Delaney 23
Monkey Wrench 25
New Years 25
Sana Wara Sana 22
Save Room 22
Somebody Told Me 24
Strings 24
Summersong 22
The Guilty Ones 23
The Wild Goose 22
This Ain't Goodbye 23
This Night 22
Walking On Sunshine 23
Wicked Ways 23
Wounded 23
=================================================
(1) Song name (2) Play count (3) Quit : 3
=================================================
Warning! The following songs are very long:
American Pie 8:34
Caves 8:19
Homecoming 9:18
Immortan’s Citadel 8:41
Jesus of Suburbia 9:08
Made For Each Other 8:01
Piano Variations 9:43
Pärt: Für Alina 10:47
Pärt: Für Alina 10:53
Sonata in B flat 15:07
The Dawning 8:04
To get the information required in this program, you will have to search through the music information. For the first type of search (searching for data about a specific song), you MUST use binary search. For many of the other parts, since the data is only sorted alphabetically by song name, you will have to use a linear search. You are not allowed to use any of Python’s built-in search functions for ANY of the searching.
Here are the specifications for this program and a few helpful tips. Make sure your program meets all of these specifications.
You should use top-down design for this program! Design your main()
function and any helper functions. Your main()
function should be relatively short and easy to read. Implement and test the program one function at a time. A good program design and good use of functions is part of the grade.
USER INPUT: When asking the user to specify a music library (A,B,or C) or an option (1,2, or 3), if they don’t specify one of these options, allow them to choose again until they get it right. In other words, your program shouldn’t crash if they do something wrong. For songs, they can specify any string, so you don’t need to do any checks or loops for that part. You may also assume the user will enter an integer for the min/max play counts.
Read in all the data for all songs and store it in a list-of-lists, like this:
[[data for song 1], [data for song 2], ...]
Here’s what the inner lists for each song should contain:
[song name, artist, album, length of song, play count, last played]
The data for each song should be stored in the correct format. For example, the song name should be a string, the play count should be an int, etc.
["Schoolin' Life", "Beyoncé", "4 (Deluxe Edition)", 293, 30, "8/18/15; 4:25 PM"]
You need to figure out how to calculate the length of the song in “minute:second” format for display purposes (hint: use the mod operator). Try to get the “0” to show up before minutes less than 10, but is it okay to omit this (i.e. for 10:08, you can show 10:8).
Refer to your class notes, exercises, and the assigned reading for tips about storing and retrieving data in a list of lists, and reading and parsing data from files.
You are free to format the output of your program any way you want, but it should be clear, concise, and easy to read.
When asking the user for a song, your program should display all information for the user-chosen song. This should work independent of case. If the song is not listed in the data, your program should say that. We recommend that during comparison only, you convert song titles to lower case:
To convert a string to a different case, you can use the .lower()
or .upper()
methods, which return the given string in all lower or upper case, respectively. These methods do not change the original string.
You are not allowed to use any of Python’s built-in search functions.
Here is an example of right-justifying a list of strings. The “10” here means format each string so that its total length is 10:
ninja_lst = ['Ayaka', 'Christie', 'Kendre', 'Kenny', 'Maleyah', 'Mikey','Rohan',
'Shayne', 'Sky', 'Tai', 'Tristan', 'Zach']
for ninja in ninja_lst:
print("%10s" % ninja)
Output:
Ayaka
Christie
Kendre
Kenny
Maleyah
Mikey
Rohan
Shayne
Sky
Tai
Tristan
Zach
We highly recommend that you show your design to an instructor or ninja before you start to implement this lab. You don’t need to submit your design as a separate document.
Here is another example run of the program, with a variety of user inputs.
Once you have fully tested all your functions and are confident your program works, fill out the questionnaire in QUESTIONS-08.txt
. Then run handin21
a final time to make sure you have submitted the most recent version of your file.