Skip to content

Instantly share code, notes, and snippets.

/jukebox.rb Secret

Created August 3, 2010 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b3cda446e3432e8cb34b to your computer and use it in GitHub Desktop.
Save anonymous/b3cda446e3432e8cb34b to your computer and use it in GitHub Desktop.
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment