A music app keeps playlists: songs in the order they were added. Build it from two classes that work together. A Song holds one song's data. A Playlist keeps a list of Song objects and answers questions about them.
Implement:
Song, created bySong::new(title, seconds), holds a song'stitleand its length inseconds;Playlist, created byPlaylist::new(), starts with no songs. Its methods:add(song)adds aSongat the end of the playlist;remove(title)removes the first song with that title and returnstrue; if no song has that title, it changes nothing and returnsfalse;total_seconds()returns the sum of the songs' lengths in seconds (0 for an empty playlist);duration()returns the same total as a string"m:ss": the minutes, a colon, then the seconds with two digits. 5 seconds are"0:05", 750 seconds are"12:30", and 3600 seconds are"60:00": the minutes are never padded.
Rust has no classes: Song and Playlist are structs with a new function and an impl block. add and remove take &mut self because they change the playlist; the other two only read it.
The input is a list of operations, applied in order to one new playlist. The output lists what the remove, total_seconds and duration calls return, in order, with booleans printed as True and False.
Example 1:
Input:
add(Song::new("Intro", 65))
add(Song::new("Sunrise", 200))
total_seconds()
duration()
remove("Intro")
duration()
Output: 265 4:25 True 3:20
Explanation: The two songs last 65 + 200 = 265 seconds: 4 minutes (240 seconds) and 25 seconds.
After "Intro" is removed, only the 200 seconds of "Sunrise" are left: 3 minutes and 20 seconds.
Example 2:
Input:
duration()
remove("Rain")
total_seconds()
Output: 0:00 False 0
Explanation: The playlist is empty: it lasts 0 seconds, and there is no "Rain" to remove.
Example 3:
Input:
add(Song::new("Echo", 30))
add(Song::new("Echo", 45))
add(Song::new("Rain", 5))
remove("Echo")
total_seconds()
remove("Echo")
remove("Echo")
duration()
Output: True 50 True False 0:05
Explanation: Two songs are called "Echo". The first remove takes only the first one, the 30 seconds,
so 45 + 5 = 50 seconds are left. The second remove takes the other "Echo", and the third finds none.
Only "Rain" is left: 5 seconds, written 0:05.
Constraints:
1 <= seconds <= 3600- A title is one word of letters and digits. Titles are compared exactly: "Rain" and "rain" are different titles.
- At most 200 operations.
Free plan: 30 tutor messages. PRO is unlimited.
