about summary refs log tree commit diff
path: root/users/wpcarro/scratch/facebook/find-rotation-point.py
blob: 3636be4d93b50aef494faa6013b7f38ab39c0c00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from math import floor

def find_rotation(xs):
    if xs[0] < xs[-1]:
        return xs[0]
    beg, end = 0, len(xs) - 1
    found = False
    count = 10
    while not found and count >= 0:
        i = beg + floor((end - beg) / 2)
        if xs[beg] < xs[i]:
            beg = i
            i = beg + floor((end - beg) / 2)
        elif xs[beg] > xs[i]:
            end = i
        found = xs[i - 1] > xs[i]
        count -= 1
    return xs[i]


xs = [(['ptolemaic',
        'retrograde',
        'supplant',
        'undulate',
        'xenoepist',
        'zebra',
        'asymptote',
        'babka',
        'banoffee',
        'engender',
        'karpatka',
        'othellolagkage',
        ], "asymptote"),
      (['asymptote',
        'babka',
        'banoffee',
        'engender',
        'karpatka',
        'othellolagkage',
        ], "asymptote"),
      ]

for x, expected in xs:
    result = find_rotation(x)
    print(x, result)
    assert result == expected
    print("Success!")