Skip to content

Instantly share code, notes, and snippets.

@SaiMun92
Created October 18, 2019 08:55
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 SaiMun92/eb961b755f2cd800cfce5c21bb4de46b to your computer and use it in GitHub Desktop.
Save SaiMun92/eb961b755f2cd800cfce5c21bb4de46b to your computer and use it in GitHub Desktop.
FindRotationPoint
import unittest
def find_rotation_point(words):
first_word = words[0]
floor_index = 0
ceiling_index = len(words) - 1
while floor_index < ceiling_index:
guess_index = floor_index + ((ceiling_index - floor_index) // 2)
if words[guess_index] >= first_word:
floor_index = guess_index
else:
ceiling_index = guess_index
if floor_index + 1 == ceiling_index:
return ceiling_index
# Tests
class Test(unittest.TestCase):
def test_small_list(self):
actual = find_rotation_point(['cape', 'cake'])
expected = 1
self.assertEqual(actual, expected)
def test_medium_list(self):
actual = find_rotation_point(['grape', 'orange', 'plum',
'radish', 'apple'])
expected = 4
self.assertEqual(actual, expected)
def test_large_list(self):
actual = find_rotation_point(['ptolemaic', 'retrograde', 'supplant',
'undulate', 'xenoepist', 'asymptote',
'babka', 'banoffee', 'engender',
'karpatka', 'othellolagkage'])
expected = 5
self.assertEqual(actual, expected)
# Are we missing any edge cases?
unittest.main(verbosity=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment