Playlist With Skip and Back
Implement a class Playlist that plays songs from a queue and remembers what has been played. Write these methods:
Playlist()starts with an empty queue and an empty history.enqueue(song)addssongto the back of the queue. Returns how many songs are now waiting in the queue.play_next()takes the song at the front of the queue and plays it: the song goes on top of the history and becomes the current song. Returns its name, or""if the queue is empty (then nothing changes).skip()removes the song at the front of the queue without playing it, so it does not go into the history and the current song stays the same. Returns its name, or""if the queue is empty.go_back()returns to the previous song: the current song is taken off the top of the history and put back at the front of the queue, and the song below it in the history becomes the current song again. Returns the name of the new current song. If the history holds fewer than two songs, nothing changes and it returns"".get_current()returns the current song, which is the song on top of the history, or""if nothing has been played.
The same song may appear more than once in the queue and in the history. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Playlist()
enqueue("intro") returns 1
enqueue("blue") returns 2
enqueue("coda") returns 3
play_next() returns "intro"
skip() returns "blue"
play_next() returns "coda"
go_back() returns "intro"
get_current() returns "intro"
play_next() returns "coda"
go_back() returns "intro"
go_back() returns ""
blue is skipped, so the history holds intro and coda. Going back puts coda at the front of the queue and makes intro current; playing next brings coda back. The last go_back finds only one song in the history.
Constraints
songis1to8lowercase letters.- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.