Israel Tech Challenge
First question: You have a list of N songs. You want to play them all at a different random way each day. In the language of your choosing, Write a function that creates that playlist. If a Rand() function exists, feel free to use it.
Language used for the implementation : Ruby on Rails
I use 3 models :
-Song : to store the song’s title and artist,
-Playlist : to store the playlist’s title and the creation date,
-PlaylistContent : to store the songs in each playlist identified by the playlist_id attribute, the model contains the song_id and the position associated to the song in the random playlist
The user interface is the view in app/views/static_pages/home.html.erb. This view displays at left an alphabetically ordered list of the N song stored in database. At the right, this view displays the randomly generated playlist correspondig to the current day, if it has been generated, otherwise the view displays a “SHUFFLE IT” button to generate and store the daily random playlist.
The random list of the N song is generated in the “playlist_controller” through the “randomize_playlist” method.
We just use the Ruby “shuffle” method on the array containing all the songs recorded in database and that returns a random array of those songs. We could also use the SQL rand() function through scope in the song model to retrieve a random list of songs.
I take care to do a “uniqueness validation” on the playlist title attribute in order to let the user generating once a day a random playlist.