Last active
April 10, 2017 15:35
-
-
Save jaredgrady/f660ab4fa9ae82778f6156744b86fa8f to your computer and use it in GitHub Desktop.
Binary search a list of ints. Recursive implementation in Python.
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
def bin_search(L, a): | |
if (len(L) == 1): | |
return "NO" | |
mid = len(L) // 2 | |
if (a < L[mid]): | |
return bin_search(L[:mid], a) | |
elif (a > L[mid]): | |
return bin_search(L[mid:], a) | |
else: | |
return "YES" | |
def main(): | |
a = int(input('Search for: ')) | |
L = [1,3,5,7,12,18,22,38] | |
print(bin_search(L, a)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice work 👍