def main(): try: array_str = input('array: ') value = int(input('value: ')) array = list(filter(lambda x: x != '', array_str.split(' '))) array = list(map(lambda s: int(s), array)) except ValueError: print('Error: Invalid arguments.') if not all(array[i] <= array[i+1] for i in range(len(array) - 1)): print('Error: Array not sorted') return 2 idx = binary_search(array, value) if idx >= 0: print('Found. Index: {}'.format(idx)) return 1 else: print('Not found.') return 0
def test_perf_search_rec1(): global perfLIST bcList = [] print(perfLIST) for i in range(measure_param[0], measure_param[1], measure_param[2]): SETUP_CODE = ''' from bsearch import binary_search from random import randint''' for find in [i // 2, i - 1, -1]: TEST_CODE = ''' binary_search(0,%d,%d)''' % (i, find) bc = (timeit.timeit(setup=SETUP_CODE, stmt=TEST_CODE, number=1000)) print( 'The Parameters of the test are find : %d - length %d - Time : %f' % (find, i, bc)) bcList.append(bc) print(bcList)
def test_even_len_res_False(): assert binary_search([0, 3, 4, 26, 52, 90, 96, 99, 120, 123], -1) == False
def test_odd_len_res_False(): assert binary_search([0, 3, 4, 26, 52, 90, 99, 120, 123], 1345) == False
def test_even_len_res_True(): assert binary_search([0, 3, 4, 26, 52, 90, 96, 99, 120, 123], 0) == True
def test_odd_len_res_True(): assert binary_search([0, 3, 4, 26, 52, 90, 99, 120, 123], 99) == True