Create a gist now

Instantly share code, notes, and snippets.

anonymous /skip_tracks.rb
Created Jul 31, 2010

What would you like to do?
# tequila version
def skip_tracks(arr, i)
start = inc = i % arr.size()
count = 1
while inc > 0 && count < arr.size() do
c = start
while (c + inc) % arr.size != start
before = (c - inc) % arr.size()
arr[before], arr[c] = arr[c], arr[before]
count = count + 1
c = c + 1
end
count = count + 1
start = start - 1
end
end
# short version (works with both Ruby 1.8 and Ruby 1.9)
def skip_tracks_18(arr, i)
t = (s = arr.size) - (h = i % s)
arr[0, t], arr[t..s] = arr[h..s], arr[0, h]
end
# shortest version (only works with Ruby 1.9)
def skip_tracks_19(arr, i)
arr.rotate! i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment