Created
June 21, 2015 00:15
-
-
Save wmantly/a23c887fc215a46b28cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/python3 | |
| from numbers import Number | |
| def binary_search( search, match ): | |
| if len( search ) < 10: | |
| print(search) | |
| for i in search: | |
| print(i) | |
| if i == match: | |
| return True | |
| return False | |
| mid = int( len( search )//2 ) | |
| print( 'mid:', mid) | |
| if isinstance( match, Number ): | |
| print('Match is a number') | |
| if search[mid:][0] <= match: | |
| print( 'mid if:', search[mid:][0]) | |
| return binary_search( search[mid:], match ) | |
| else: | |
| search[:mid][0] | |
| return binary_search( search[:mid], match ) | |
| elif type( match ) == str: | |
| print('Match is a string') | |
| if search[mid:][0][0] <= match[0]: | |
| return binary_search( search[mid:], match ) | |
| else: | |
| return binary_search( search[:mid], match ) | |
| else: | |
| print('This funtion only takes number(int or float) and strings') | |
| return False | |
| print('...') | |
| ##sample dataset | |
| arr = [1,3,9,11,23,44,66,88,102,142,188,192,239,382,492,1120,1900,2500,4392,5854,6543,8292,9999,29122] | |
| print ( binary_search(arr, 44) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment