secret
anonymous / jukebox.rb
Created

  • Download Gist
jukebox.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
def skip_tracks(arr, i)
start = inc = i % arr.size()
count = 1
while inc > 0 && count < arr.size() do
c = start
begin
before = (c - inc) % arr.size()
arr[before], arr[c] = arr[c], arr[before]
count = count + 1
c = (c + inc) % arr.size()
end while (c + inc) % arr.size() != start
count = count + 1
start = start - 1
end
end
 
def skip_tracks_new_version(playlist, tracks)
return if tracks.abs >= playlist.size or tracks.zero?
if tracks > 0
playlist.push(playlist.shift(tracks)).flatten!
else
playlist.unshift(playlist.pop(tracks.abs)).flatten!
end
end
jukebox_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
require 'jukebox'
 
describe "jukebox" do
before do
@playlist = %w[song1 song2 song3 song4 song5 song6 song7 song8 song9 song10]
@variations = [
%w[song1 song2 song3 song4 song5 song6 song7 song8 song9 song10],
%w[song2 song3 song4 song5 song6 song7 song8 song9 song10 song1],
%w[song3 song4 song5 song6 song7 song8 song9 song10 song1 song2],
%w[song4 song5 song6 song7 song8 song9 song10 song1 song2 song3],
%w[song5 song6 song7 song8 song9 song10 song1 song2 song3 song4],
%w[song6 song7 song8 song9 song10 song1 song2 song3 song4 song5],
%w[song7 song8 song9 song10 song1 song2 song3 song4 song5 song6],
%w[song8 song9 song10 song1 song2 song3 song4 song5 song6 song7],
%w[song9 song10 song1 song2 song3 song4 song5 song6 song7 song8],
%w[song10 song1 song2 song3 song4 song5 song6 song7 song8 song9]
]
end
describe "initial implementation" do
it "should not skip 0" do
skip_tracks(@playlist, 0)
@playlist.should == @variations[0]
end
it "should skip forward" do
(1..9).each do |n|
skip_tracks(@playlist, n)
@playlist.should == @variations[n]
@playlist = %w[song1 song2 song3 song4 song5 song6 song7 song8 song9 song10]
end
end
it "should not skip 'playlist' length forward" do
skip_tracks(@playlist, @playlist.length)
@playlist.should == @variations[0]
end
it "should not skip pass end forward" do
# this fails in the initial impl
end
it "should skip backward" do
(1..9).each do |n|
skip_tracks(@playlist, -n)
@playlist.should == @variations[-n]
@playlist = %w[song1 song2 song3 song4 song5 song6 song7 song8 song9 song10]
end
end
it "should not skip 'playlist' length backward" do
skip_tracks(@playlist, -@playlist.length)
@playlist.should == @variations[0]
end
 
it "should not skip pass end backward" do
# this fails in the initial impl
end
end
describe "new implementation" do
it "should not skip 0" do
skip_tracks_new_version(@playlist, 0)
@playlist.should == @variations[0]
end
it "should skip forward" do
(1..9).each do |n|
skip_tracks_new_version(@playlist, n)
@playlist.should == @variations[n]
@playlist = %w[song1 song2 song3 song4 song5 song6 song7 song8 song9 song10]
end
end
it "should not skip 'playlist' length forward" do
skip_tracks_new_version(@playlist, -@playlist.length)
@playlist.should == @variations[0]
end
it "should not skip pass end forward" do
skip_tracks_new_version(@playlist, 11)
@playlist.should == @variations[0]
end
it "should skip backward" do
(1..9).each do |n|
skip_tracks_new_version(@playlist, -n)
@playlist.should == @variations[-n]
@playlist = %w[song1 song2 song3 song4 song5 song6 song7 song8 song9 song10]
end
end
it "should not skip 'playlist' length backward" do
skip_tracks_new_version(@playlist, -@playlist.length)
@playlist.should == @variations[0]
end
it "should not skip pass end backward" do
skip_tracks_new_version(@playlist, -11)
@playlist.should == @variations[0]
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.