Last active
March 8, 2016 23:22
-
-
Save celeryclub/6789a48d05e651b2344f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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