Editor

Add to the Playlist

#598 · Functions · Intermediate · Find and fix · 70 XP · about 10 min

add_song(song, playlist) adds a song to the end of a playlist and returns the playlist. The playlist argument is optional: without one, the function starts a brand-new, empty playlist. When a playlist is given, that same list is changed and returned.

Two more rules: a song that is already in the playlist is not added again, and a playlist holds at most 5 songs, so adding a song to a full playlist first drops the oldest song (the first one).

The code in the editor has two bugs. The first call without a playlist works, but every later call without one gives back the songs from the earlier calls too. And a full playlist grows to six songs.

Examples

add_song('Intro')                            # returns ['Intro']
add_song('Outro')                            # returns ['Outro']
add_song('c', ['a', 'b'])                    # returns ['a', 'b', 'c']
add_song('a', ['a', 'b'])                    # returns ['a', 'b']
add_song('f', ['a', 'b', 'c', 'd', 'e'])     # returns ['b', 'c', 'd', 'e', 'f']

Constraints

  • A playlist given to the function has at most 5 songs.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.