In python. Please do not copy from other websites, as it is incorrect and I'd like to be able to apply this to future problems. Thanks. Alter the binary search function below so that, instead of returning -1 when the target value is not in the list, it raises a TargetNotFound exception (you'll need to define this exception class). Otherwise it should function as normal. def binary_search(a_list, target): """ Searches a_list for an occurrence of target If found, returns the index of its position in the list If not found, returns -1, indicating the target value isn't in the list """ first = 0 last = len(a_list) - 1 while first <= last: middle = (first + last) // 2 if a_list[middle] == target: return middle if a_list[middle] > target: last = middle - 1 else: first = middle + 1 return -1

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter12: Exception Handling
Section: Chapter Questions
Problem 2GZ
icon
Related questions
Question

In python.

Please do not copy from other websites, as it is incorrect and I'd like to be able to apply this to future problems. Thanks. Alter the binary search function below so that, instead of returning -1 when the target value is not in the list, it raises a TargetNotFound exception (you'll need to define this exception class). Otherwise it should function as normal.

def binary_search(a_list, target):
"""
Searches a_list for an occurrence of target
If found, returns the index of its position in the list
If not found, returns -1, indicating the target value isn't in the list
"""
first = 0
last = len(a_list) - 1
while first <= last:
middle = (first + last) // 2
if a_list[middle] == target:
return middle
if a_list[middle] > target:
last = middle - 1
else:
first = middle + 1
return -1

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Custom Exception class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning