class BinarySearchTest(TestCase): """Get the index of the item with an expected number of loops in\ array [1, 2 . . . 20] Returns a dictionary containing {count: value, index: value} """ def setUp(self): self.one_to_twenty = BinarySearch(20, 1) self.two_to_forty = BinarySearch(20, 2) self.ten_to_thousand = BinarySearch(100, 10) def test_small_list_search(self): search = self.one_to_twenty.search(16) self.assertGreater(5, search['count'], msg='should return {count: 4, index: 15} for 16') self.assertEqual(15, search['index'], msg='should return {count: 4, index: 15} for 16') def test_medium_list_search(self): search1 = self.two_to_forty.search(16) search2 = self.two_to_forty.search(40) search3 = self.two_to_forty.search(33) self.assertGreater(5, search1['count'], msg='should return {count: 4, index: 7} for 16') self.assertEqual(7, search1['index'], msg='should return {count: 4, index: 7} for 16') self.assertEqual(0, search2['count'], msg='should return {count: 0, index: 19} for 40') self.assertEqual(19, search2['index'], msg='should return {count: 5, index: 19} for 40')
def test_negative_positive(): data = [i - 100 for i in range(200)] # Ensure the search finds every element in the list of test data. for i in range(len(data)): bin_search = BinarySearch(data, data[i]) assert bin_search.was_found() assert bin_search.index == i
def test_binary_search_one_element(self): array = [20] binary = BinarySearch(array) self.assertIsNotNone(binary.array) self.assertEqual(len(binary.array), 1) self.assertEqual(binary.binary_search_algorithm(0, len(binary.array) - 1, 20), 0)
def test_binary_search_odd_number_elements(self): array = [20, 30, 40, 50, 99] binary = BinarySearch(array) self.assertIsNotNone(binary.array) self.assertEqual(len(binary.array), 5) self.assertEqual(binary.binary_search_algorithm(0, len(binary.array) - 1, 99), 4)
def test_find_other_type(): data = [ 'A', 'C', 'F', 'X', 'Z', 'a', 'b', 'i', 'k', 'm', 'q', 'r', 'u', 'v' ] for i in range(len(data)): bin_search = BinarySearch(data, data[i]) assert bin_search.was_found() assert bin_search.index == i
def test_binary_search_empty_array(self): array = random.sample(range(1, 100), 0) binary = BinarySearch(array) self.assertIsNotNone(binary.array) self.assertEqual(len(binary.array), 0) binary.array.sort() self.assertEqual(binary.binary_search_algorithm(0, len(binary.array) - 1, 3), -1)
def test_binary_search(): numbers = [0,3,5,12,17,23,34,78,134] sut = BinarySearch(numbers) eq_(sut.search(0), 0) eq_(sut.search(3), 1) eq_(sut.search(17), 4) eq_(sut.search(134), 8) eq_(sut.search(135), -1)
def test_two_elements(): data = [0, 1] # Found first element bin_search = BinarySearch(data, 0) assert bin_search.was_found assert bin_search.index == 0 # Found second element bin_search = BinarySearch(data, 1) assert bin_search.was_found assert bin_search.index == 1
def test_recursive_bs_simple(self): arr = sorted(TestData.TO_SEARCH) item, expected_index = 45, 5 index = BinarySearch.recursive_impl(arr, item, 0, len(arr)) self.assertEqual(expected_index, index) item, expected_index = 105, -1 index = BinarySearch.recursive_impl(arr, item, 0, len(arr)) self.assertEqual(expected_index, index)
def test_correct_partial_insert_index(): data = [2, 4, 6, 8, 10, 12] input_data = [3, 5, 7, 9, 11, 13] # Ensure the input items are not found, then insert them. for item in input_data: bin_search = BinarySearch(data, item) assert not bin_search.was_found() data.insert(bin_search.index, item) expected_data = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] # Ensure the inserted data matches the expected data. assert len(data) == len(expected_data) for i in range(len(expected_data)): assert data[i] == expected_data[i]
def test_correct_insert_index(): data = [] input_data = [4, 1, 3, 7, 8, 6, 2, 0, 9] # Ensure the input items are not found, then insert them. for item in input_data: bin_search = BinarySearch(data, item) assert not bin_search.was_found() data.insert(bin_search.index, item) expected_data = [0, 1, 2, 3, 4, 6, 7, 8, 9] # Ensure the inserted data matches the expected data. assert len(data) == len(expected_data) for i in range(len(expected_data)): assert data[i] == expected_data[i]
class BinarySearchTest(unittest.TestCase): def setUp(self): self.BinS = BinarySearch([11, 19, 27, 33, 42, 57, 63, 76, 81, 93, 99 ]) #pass the list while creating the object def tearDown(self): pass def test_binary_search_found(self): self.assertEqual(self.BinS.binary_search(27), 2) #random element self.assertEqual(self.BinS.binary_search(11), 0) #first element self.assertEqual(self.BinS.binary_search(99), 10) #last element def test_binary_search_notFound(self): self.assertEqual(self.BinS.binary_search(53), -1) #test for element which is not in the set
def test_long_length_odd(): data = [i * 2 for i in range(10001)] # Ensure the search finds every element in the list of test data. for i in range(len(data)): bin_search = BinarySearch(data, data[i]) assert bin_search.was_found assert bin_search.index == i
def test_short_length_odd(): data = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21] # Ensure the search finds every element in the list of test data. for i in range(len(data)): bin_search = BinarySearch(data, data[i]) assert bin_search.was_found assert bin_search.index == i
def test_insert_other_type(): data = [] input_data = [ 'i', 'A', 'm', 'F', 'u', 'r', 'Z', 'a', 'C', 'k', 'X', 'q', 'b', 'v' ] # Ensure the input items are not found, then insert them. for item in input_data: bin_search = BinarySearch(data, item) assert not bin_search.was_found() data.insert(bin_search.index, item) expected_data = [ 'A', 'C', 'F', 'X', 'Z', 'a', 'b', 'i', 'k', 'm', 'q', 'r', 'u', 'v' ] # Ensure the inserted data matches the expected data. assert len(data) == len(expected_data) for i in range(len(expected_data)): assert data[i] == expected_data[i]
def get_all_solutions(a): solutions = [] sorted_a = sorted(a) n = len(a) for i in range(0, n): j = BinarySearch.binary_search(sorted_a, -a[i]) if j > i: solutions.append((a[i], a[j])) return solutions
def test_binary_search_and_linear_search_execution_time(self): item, expected_index = self.expected_item, self.expected_index start_time = time.time() bs_iterative_founded_index = BinarySearch.iterative_impl( self.arr, item) bsi_time = time.time() - start_time start_time = time.time() bs_recursive_founded_index = BinarySearch.recursive_impl( self.arr, item, 0, len(self.arr)) bsr_time = time.time() - start_time start_time = time.time() ls_founded_index = self.arr.index(item) bs_linear_time = time.time() - start_time self.assertEqual(expected_index, bs_iterative_founded_index) self.assertEqual(expected_index, bs_recursive_founded_index) self.assertEqual(expected_index, ls_founded_index)
def get_all_solutions(a): solutions = [] sorted_a = sorted(a) n = len(a) for i in range(0, n): for j in range(i + 1, n): k = BinarySearch.binary_search(sorted_a, -(a[i] + a[j])) if k > j: solutions.append((a[i], a[j], a[k])) return solutions
def test_search_search_key(): # Create a test list and a search list to test the search key. data = [(i * 2) + 5 for i in range(1000)] search = [BasicObject(i * 2) for i in range(1000)] # Ensure the search item is found in the data after the search key # is applied. for i in range(len(data)): bin_search = BinarySearch(data, search[i], search_key=lambda x: x.operate()) assert bin_search.was_found assert bin_search.index == i
def test_bin_search(test_args, expected): """The list is first sorted with a custom quick search, then a binary search occurs param: n - Length of entered list (int) param: b_search - result of binary search """ print(test_args) n = len(test_args) - 1 test_sort = QuickSort() test_sort.quickSort(test_args, 0, n) b_search = BinarySearch().bin_search(test_args, 12) assert b_search == expected
def test_not_found_odd_edges(): data = [i + 10 for i in range(101)] # Ensure one less than lowest element was not found. bin_search = BinarySearch(data, 9) assert not bin_search.was_found() # Ensure one more than highest element was not found. bin_search = BinarySearch(data, 111) assert not bin_search.was_found()
def test_null_list(): bin_search = BinarySearch(None, 0) assert not bin_search.was_found() assert bin_search.index == 0 bin_search = BinarySearch(None, 15) assert not bin_search.was_found() assert bin_search.index == 0
def test_empty_list(): data = [] bin_search = BinarySearch(data, 0) assert not bin_search.was_found() assert bin_search.index == 0 data = [] bin_search = BinarySearch(data, 15) assert not bin_search.was_found() assert bin_search.index == 0
from queue_ import Queue from linear import Array from stack import Stack from bubble import BubbleSort from linked_list import LinkedList from binary_search import BinarySearch from bubble import BubbleSort from selection import SelectionSort from binary_tree import BinaryTree # all objects of the classes larr = Array() stk = Stack(10) quez = Queue() llist = LinkedList() binary = BinarySearch() bubble = BubbleSort() selection = SelectionSort() bitree = BinaryTree() def main_menu(): print("\v") text = colored(' ALGORITHMS AND DATA STRUCTURES ','magenta','on_cyan',['blink']) print("\t\t\t\t",text) print("\v") AL = { 1:"Array",2:"stacks",3:"Queue",4:"Linked list",5:"Binary trees", 6:"binary Search",7:"BubbleSort",8:"insertion sort",9:"selection sort"} for k,v in AL.items(): print("{} {}".format(k,v))
def test_binary_search_medium_list(): # test large list new_search = BinarySearch(10, 3) assert new_search.search(5) == {'work_done': 2}
def test_binary_search_medium_list(): # test medium list new_search = BinarySearch(5, 2) assert new_search.search(8) == {'work_done': 2}
def test_binary_search_small_list(): # test small list new_search = BinarySearch(3, 1) assert new_search.search(3) == {'work_done': 1}
class TestBinarySearch(unittest.TestCase): """ Test cases for finding if the search function gets the index of the item with an expected number of loops in an array and returns a dictionary containing {count: value, index: value} """ # Instantiating objects for using in the tests def setUp(self): self.one_to_twenty = BinarySearch(20, 1) self.two_to_forty = BinarySearch(20, 2) self.ten_to_thousand = BinarySearch(100, 10) # Testing if the function returns {count: 4, index: 15} for input 16 in a small sized list def test_small_list_search(self): search = self.one_to_twenty.search(16) self.assertGreater(5, search['count'], msg='should return {count: 4, index: 15} for 16') self.assertEqual(15, search['index'], msg='should return {count: 4, index: 15} for 16') # Testing if the function returns the right results for different inputs in a medium sized list def test_medium_list_search(self): search1 = self.two_to_forty.search(16) search2 = self.two_to_forty.search(40) search3 = self.two_to_forty.search(33) self.assertGreater(5, search1['count'], msg='should return {count: 4, index: 7} for 16') self.assertEqual(7, search1['index'], msg='should return {count: 4, index: 7} for 16') self.assertEqual(5, search2['count'], msg='should return {count: 5, index: 19} for 40') self.assertEqual(19, search2['index'], msg='should return {count: 5, index: 19} for 40') self.assertGreater(4, search3['count'], msg='should return {count: 3, index: -1} for 33') self.assertEqual(-1, search3['index'], msg='should return {count: 3, index: -1} for 33') # Testing if the function returns the right results for different inputs in a large sized list def test_large_list_search(self): search1 = self.ten_to_thousand.search(40) search2 = self.ten_to_thousand.search(880) search3 = self.ten_to_thousand.search(10000) self.assertGreater( 7, search1['count'], msg='should return {count: # <= 7, index: 3} for 40') self.assertEqual(3, search1['index'], msg='should return {count: # <= 7, index: 3} for 40') self.assertGreater( 4, search2['count'], msg='should return {count: # <= 3, index: 87} for 880') self.assertEqual( 87, search2['index'], msg='should return {count: # <= 3, index: 87} for 880') self.assertGreater(7, search3['count'], msg='should return {count: 3, index: -1} for 10000') self.assertEqual(-1, search3['index'], msg='should return {count: 3, index: -1} for 10000')
def fill_arr(n): list_res = [] for i in range(n): list_res.append(randint(MIN_VALUE, MAX_VALUE)) return list_res array = fill_arr(n) from time import time # Запуск алгоритма from linear_search import LinearSearch linear = LinearSearch(n, array) start_time = time() result_index = linear.linear_search(x) print('Индекс заданного числа в массиве:', result_index, '\n', f'{(time() - start_time) * 1000:.3e}') from binary_search import BinarySearch array.sort() binary = BinarySearch(n, array) start_time = time() result_index = binary.binary_search(x) print('Индекс заданного числа в массиве:', result_index, '\nРезультирующее время:', f'{(time() - start_time) * 1000:.3e}') from binary_search_1 import BinarySearch_1 binary = BinarySearch_1(n, array) start_time = time() result_index = binary.binary_search(x) print('Индекс заданного числа в массиве:', result_index, '\nРезультирующее время:', f'{(time() - start_time) * 1000:.3e}')
from binary_search import BinarySearch import unittest import json import time bs = BinarySearch() # Unloading all lists from a file with open("items.json", "r") as file: data = json.load(file) # Setting values to created variables simple_list = data["simple_list"] list_with_10_items = data["list_with_10_items"] list_with_100_items = data["list_with_100_items"] list_with_1000_items = data["list_with_1000_items"] # Test cases to test Binary Search algorithm class TestBinarySearch(unittest.TestCase): def setUp(self): print(".......... %s" % self._testMethodName) # Checking the implementation of iterative binary search def test_iterative_binary_search_with_simple_list(self): # ARRANGE # You can check the index of each item in the items.json file item, expected_index = 3, 1 # ACT # Run the method we created and get the index of the item we were looking for
def test_it_should_search_the_right_side(self): binary_search = BinarySearch(self._ordered_list) assert binary_search.find(17) == True
def setUp(self): self.BinS = BinarySearch([11, 19, 27, 33, 42, 57, 63, 76, 81, 93, 99 ]) #pass the list while creating the object
def setUp(self): self.one_to_twenty = BinarySearch(20, 1) self.two_to_forty = BinarySearch(20, 2) self.ten_to_thousand = BinarySearch(100, 10)
def test_it_should_return_false_if_val_not_in_list_low_val(self): binary_search = BinarySearch(self._ordered_list) assert binary_search.find(0) == False