Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2010 11:42
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/502083 to your computer and use it in GitHub Desktop.
Save anonymous/502083 to your computer and use it in GitHub Desktop.
# 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