Mercurial
diff grok_interview/binary_search.py @ 60:d64a8c189a77
Merged
| author | June Park <me@mrjunejune.com> |
|---|---|
| date | Sat, 20 Dec 2025 13:56:01 -0500 |
| parents | 68fa88ac73fe |
| children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/grok_interview/binary_search.py Sat Dec 20 13:56:01 2025 -0500 @@ -0,0 +1,18 @@ + +x = [1,2,3,4,5,9,20,25,33,99] +# | | + +target = 18 +left = 0 +right = len(x) + +while left < right: + mid = (left + right)//2 + if x[mid] == target: + break + elif x[mid] < target: + left = mid + 1 + else: + right = mid + +print(x[mid])