secret
anonymous / ruby_magnets.rb
Last active

  • Download Gist
gistfile3.txt
1 2
#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
ruby_magnets.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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
ruby_magnets_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
#!/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
skip_tracks.rb
Ruby
1
def skip_tracks(a,i) o=i%a.size; a.replace(a[o..-1]+a[0..o-1]) if o; end
skip_tracks_favourite.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
#!/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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.