In python    Code so far: # TODO: Declare global variables here. recursions = 0 comparisons = 0 def binary_search(nums, target, lower, upper):     # Type your code here.     target = (lower + upper) //2     global           if target == nums[index]:         return index     if (lower == upper):         comparisons +=1         if target == nums[lower]:             return lower         else:             return -1     if (nums[index] < target):         recursions += 1         return binary_search(nums,target, index, upper)     if nums[index] > target:         recursions += 1         return binary_search(nums, target, 0, lower) if __name__ == '__main__':     # Input a list of nums from the first line of input     nums = [int(n) for n in input().split()]          # Input a target value     target = int(input())     # Start off with default values: full range of list indices     index = binary_search(nums, target, 0, len(nums) - 1)     # Output the index where target was found in nums, and the     # number of recursions and comparisons performed     print(f'index: {index}, recursions: {recursions}, comparisons: {comparisons}')

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

In python 

 

Code so far:

# TODO: Declare global variables here.
recursions = 0
comparisons = 0


def binary_search(nums, target, lower, upper):
    # Type your code here.
    target = (lower + upper) //2
    global 
    
    if target == nums[index]:
        return index
    if (lower == upper):
        comparisons +=1
        if target == nums[lower]:
            return lower
        else:
            return -1
    if (nums[index] < target):
        recursions += 1
        return binary_search(nums,target, index, upper)
    if nums[index] > target:
        recursions += 1
        return binary_search(nums, target, 0, lower)


if __name__ == '__main__':
    # Input a list of nums from the first line of input
    nums = [int(n) for n in input().split()]
    
    # Input a target value
    target = int(input())

    # Start off with default values: full range of list indices
    index = binary_search(nums, target, 0, len(nums) - 1)

    # Output the index where target was found in nums, and the
    # number of recursions and comparisons performed
    print(f'index: {index}, recursions: {recursions}, comparisons: {comparisons}')

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

I'm getting this error for the above code

1: Compare output
Output differs. See highlights below.
Input
Your output
Expected output
2: Compare output
Input
Your output
Output differs. See highlights below.
Expected output
3: Compare output
Input
Your output
Expected output
4: Compare output
1 2 3 4 5 6 7 8 9
2
index: 1, recursions: 1, comparisons: 1
Output differs. See highlights below.
Input
index: 1, recursions: 2, comparisons:
Your output
11 22 33 44 55 66 77 88 99
11
index: 0, recursions: 2, comparisons: 1
index: 0, recursions: 3, comparisons: 5
10 15 20 25 30 35 40 45
50
index: -1, recursions: 4, comparisons:
Output differs. See highlights below.
index: -1, recursions: 4, comparisons: 7
10 20 20 20 20 20 25 30 35 40 45 50 60
20
index: 2, recursions: 1, comparisons:
Expected output index: 2, recursions: 2, comparisons:
5: Unit test A
Test binary_search() with [11, 22, 33, 44, 55, 66, 77, 88, 99] and target = 99. Should return 8.
Test feedback binary_search() correctly returned 8
0/2
0/2
0/2
0/2
2/2
Transcribed Image Text:1: Compare output Output differs. See highlights below. Input Your output Expected output 2: Compare output Input Your output Output differs. See highlights below. Expected output 3: Compare output Input Your output Expected output 4: Compare output 1 2 3 4 5 6 7 8 9 2 index: 1, recursions: 1, comparisons: 1 Output differs. See highlights below. Input index: 1, recursions: 2, comparisons: Your output 11 22 33 44 55 66 77 88 99 11 index: 0, recursions: 2, comparisons: 1 index: 0, recursions: 3, comparisons: 5 10 15 20 25 30 35 40 45 50 index: -1, recursions: 4, comparisons: Output differs. See highlights below. index: -1, recursions: 4, comparisons: 7 10 20 20 20 20 20 25 30 35 40 45 50 60 20 index: 2, recursions: 1, comparisons: Expected output index: 2, recursions: 2, comparisons: 5: Unit test A Test binary_search() with [11, 22, 33, 44, 55, 66, 77, 88, 99] and target = 99. Should return 8. Test feedback binary_search() correctly returned 8 0/2 0/2 0/2 0/2 2/2
Solution
Bartleby Expert
SEE SOLUTION
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY