Skip to content

Instantly share code, notes, and snippets.

@celeryclub
Last active March 8, 2016 23:22
Show Gist options
  • Save celeryclub/6789a48d05e651b2344f to your computer and use it in GitHub Desktop.
Save celeryclub/6789a48d05e651b2344f to your computer and use it in GitHub Desktop.
infile = ARGV[ 0 ]
words = File.readlines( infile )
# O(log n)
def rotation_index( array, lowest_so_far = nil, from = 0, to = nil )
lowest_so_far ||= array.first.downcase
to ||= array.size - 1
if from > to
return array.size == from ? 0 : from
end
mid = ( from + to ) / 2
current = array[ mid ].downcase
if current > lowest_so_far
rotation_index array, lowest_so_far, mid + 1, to
elsif current < lowest_so_far
rotation_index array, current, 0, mid - 1
end
end
puts rotation_index( words )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment