Created
October 18, 2019 08:55
-
-
Save SaiMun92/eb961b755f2cd800cfce5c21bb4de46b to your computer and use it in GitHub Desktop.
FindRotationPoint
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
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