Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Define a function called binary search_tuples (tuples_list, target_tuple) which takes a sorted list of tuples and a tuple as parameters. The function searches for the
Define a function called binary search_tuples (tuples_list, target_tuple) which takes a sorted list of tuples and a tuple as parameters. The function searches for the given tuple in a sorted list of tuples using a Binary Search. The function first computes an index mid_index, which is the index of the middle element (rounded down if index is not an integer) and then compares the search tuple with the element at that index. If they are equal, the search terminates, otherwise the search continues in the left or right sub- list, depending on whether the search tuple is smaller or larger than tuples_list(mid_index]. The function should return a tuple containing the index position in the list if found and the number of mid_index calculations required in searching; and -1 and the number of mid_index calculations the search required if not found. For educational purposes, the function should also print the left index, right index and mid index in each searching step. Note: You can assume that the sorted list is not empty. You may not use Python's built in index0 or find0 methods. For example: Test Result my_list = [(1,5)] a tuple = (3,1) print(binary_search_tuples(my_list, a_tuple)) left: 0; right: 0, mid: a 7-1, 1) my_list = [(1,5),(1,7), (2,1),(2,5),(3,1), (3,5)] a_tuple = (3,1) print(binary_search_tuples(my_list, a_tuple)) left: nights, mid: 2 left: 3. right: 5, mid: 4 (4,2)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started