Mercurial
comparison grok_interview/binary_search.py @ 51:68fa88ac73fe
Interview prep for xAI
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 15 Dec 2025 19:55:17 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 46:b9a40c633c93 | 51:68fa88ac73fe |
|---|---|
| 1 | |
| 2 x = [1,2,3,4,5,9,20,25,33,99] | |
| 3 # | | | |
| 4 | |
| 5 target = 18 | |
| 6 left = 0 | |
| 7 right = len(x) | |
| 8 | |
| 9 while left < right: | |
| 10 mid = (left + right)//2 | |
| 11 if x[mid] == target: | |
| 12 break | |
| 13 elif x[mid] < target: | |
| 14 left = mid + 1 | |
| 15 else: | |
| 16 right = mid | |
| 17 | |
| 18 print(x[mid]) |