Skip to content

Instantly share code, notes, and snippets.

/ruby_magnets.rb Secret

Created August 9, 2010 09:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/de84d4a517c49ef1feac to your computer and use it in GitHub Desktop.
#If we don't have to modify in place, but rather return the new playlist, this can be shortened to:
def skip_tracks(a,i) o=i%a.size; o==0 ? a : a[o..-1]+a[0..o-1] end
def skip_tracks(arr,i)
start=inc=i%arr.size
count=1
while inc > 0 && count < arr.size() do
c=start
count=count+1
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
start=start-1
end
arr
end
#!/usr/bin/env ruby
require 'rubygems'
require 'spec'
require 'ruby_magnets.rb'
PLAYLIST0=["track1", "track2", "track3", "track4", "track5", "track6", "track7"]
PLAYLIST1=["track2", "track3", "track4", "track5", "track6", "track7", "track1"]
PLAYLIST2=["track3", "track4", "track5", "track6", "track7", "track1", "track2"]
PLAYLIST3=["track4", "track5", "track6", "track7", "track1", "track2", "track3"]
PLAYLIST4=["track5", "track6", "track7", "track1", "track2", "track3", "track4"]
PLAYLIST5=["track6", "track7", "track1", "track2", "track3", "track4", "track5"]
PLAYLIST6=["track7", "track1", "track2", "track3", "track4", "track5", "track6"]
describe Object do
before(:each) do
@playlist=PLAYLIST0.dup
end
it 'should move the first track to the end with #skip_tracks(playlist,1)' do
skip_tracks(@playlist,1)
@playlist.should == PLAYLIST1
end
it 'should move the last track to the first with #skip_tracks(playlist,-1)' do
skip_tracks(@playlist,-1)
@playlist.should == PLAYLIST6
end
it 'should move the first two tracks to the end with #skip_tracks(playlist,2)' do
skip_tracks(@playlist,2)
@playlist.should == PLAYLIST2
end
it 'should move the last two tracks to the start with #skip_tracks(playlist,-2)' do
skip_tracks(@playlist,-2)
@playlist.should == PLAYLIST5
end
it 'should move the first three tracks to the end with #skip_tracks(playlist,3)' do
skip_tracks(@playlist,3)
@playlist.should == PLAYLIST3
end
it 'should move the last three tracks to the start with #skip_tracks(playlist,-3)' do
skip_tracks(@playlist,-3)
@playlist.should == PLAYLIST4
end
it 'should move the first four tracks to the end with #skip_tracks(playlist,3)' do
skip_tracks(@playlist,4)
@playlist.should == PLAYLIST4
end
it 'should move the last four tracks to the start with #skip_tracks(playlist,-3)' do
skip_tracks(@playlist,-4)
@playlist.should == PLAYLIST3
end
it 'should move the first track to the end with #skip_tracks(playlist,8)' do
skip_tracks(@playlist,8)
@playlist.should == PLAYLIST1
end
it 'should move the last track to the start with #skip_tracks(playlist,-8)' do
skip_tracks(@playlist,-8)
@playlist.should == PLAYLIST6
end
end
def skip_tracks(a,i) o=i%a.size; a.replace(a[o..-1]+a[0..o-1]) if o; end
#!/usr/bin/env ruby
require 'rubygems'
require 'spec'
require 'skip_tracks_favourite.rb'
PLAYLIST0=["track1", "track2", "track3", "track4", "track5", "track6", "track7"]
PLAYLIST1=["track2", "track3", "track4", "track5", "track6", "track7", "track1"]
PLAYLIST2=["track3", "track4", "track5", "track6", "track7", "track1", "track2"]
PLAYLIST3=["track4", "track5", "track6", "track7", "track1", "track2", "track3"]
PLAYLIST4=["track5", "track6", "track7", "track1", "track2", "track3", "track4"]
PLAYLIST5=["track6", "track7", "track1", "track2", "track3", "track4", "track5"]
PLAYLIST6=["track7", "track1", "track2", "track3", "track4", "track5", "track6"]
describe Object do
before(:each) do
@playlist=PLAYLIST0.dup
end
it 'should move the first track to the end with #skip_tracks(playlist,1)' do
skip_tracks(@playlist,1)
@playlist.should == PLAYLIST1
end
it 'should move the last track to the first with #skip_tracks(playlist,-1)' do
skip_tracks(@playlist,-1)
@playlist.should == PLAYLIST6
end
it 'should move the first two tracks to the end with #skip_tracks(playlist,2)' do
skip_tracks(@playlist,2)
@playlist.should == PLAYLIST2
end
it 'should move the last two tracks to the start with #skip_tracks(playlist,-2)' do
skip_tracks(@playlist,-2)
@playlist.should == PLAYLIST5
end
it 'should move the first three tracks to the end with #skip_tracks(playlist,3)' do
skip_tracks(@playlist,3)
@playlist.should == PLAYLIST3
end
it 'should move the last three tracks to the start with #skip_tracks(playlist,-3)' do
skip_tracks(@playlist,-3)
@playlist.should == PLAYLIST4
end
it 'should move the first four tracks to the end with #skip_tracks(playlist,3)' do
skip_tracks(@playlist,4)
@playlist.should == PLAYLIST4
end
it 'should move the last four tracks to the start with #skip_tracks(playlist,-3)' do
skip_tracks(@playlist,-4)
@playlist.should == PLAYLIST3
end
it 'should move the first track to the end with #skip_tracks(playlist,8)' do
skip_tracks(@playlist,8)
@playlist.should == PLAYLIST1
end
it 'should move the last track to the start with #skip_tracks(playlist,-8)' do
skip_tracks(@playlist,-8)
@playlist.should == PLAYLIST6
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment