diff options
author | William Carroll <wpcarro@gmail.com> | 2020-11-12T14·37+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-11-12T14·37+0000 |
commit | aa66d9b83d5793bdbb7fe28368e0642f7c3dceac (patch) | |
tree | a0e6ad240fe1cdfd2fcdba7266931beea9fbe0d6 /scratch/facebook/find-rotation-point.py | |
parent | d2d772e43e0d4fb1bfaaa58d7de0c9e2cc274a25 (diff) |
Add coding exercises for Facebook interviews
Add attempts at solving coding problems to Briefcase.
Diffstat (limited to 'scratch/facebook/find-rotation-point.py')
-rw-r--r-- | scratch/facebook/find-rotation-point.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/scratch/facebook/find-rotation-point.py b/scratch/facebook/find-rotation-point.py new file mode 100644 index 000000000000..3636be4d93b5 --- /dev/null +++ b/scratch/facebook/find-rotation-point.py @@ -0,0 +1,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!") |