Question

Python 3 Write a binary search function that, instead of returning -1 when the target value...

Python 3

Write a binary search function that, instead of returning -1 when the target value is not in the list, raises a TargetNotFound exception (you'll need to define this exception class). Otherwise it should function normally. Name this function bin_except.

The file must be named: bin_except.py

0 0
Add a comment Improve this question Transcribed image text
Answer #1

- targetnotfound.py - D:/Coding/Python/targetnotfound.py (3.7... File Edit Format Run Options Window Help #Define TargetNot F

OUTPUT :

================ RESTART: D: /Coding/Python/targetnotfound.py >>> a = [1,2,5,9,15] >>> bin_except (a, 10) Traceback (most rec

CODE :

#Define TargetNotFound exception
class TargetNotFound(Exception):
pass

#Function that performs binary search
def bin_except(a,key):
#Initialize l and r
l = 0
r = len(a)-1
#Continue till l<=r
while l<=r:
#Find mid value m
m = (l+r)//2
#Check if key is found
if a[m]==key:
return m
#Change l or r
if a[m]<key:
l = m+1
else:
r = m-1
raise TargetNotFound
  

Add a comment
Know the answer?
Add Answer to:
Python 3 Write a binary search function that, instead of returning -1 when the target value...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a binary search function that, instead of returning -1 when the target value is not...

    Write a binary search function that, instead of returning -1 when the target value is not in the list, raises a TargetNotFound exception (you'll need to define this exception class). Otherwise it should function normally. Name this function bin_except. The file must be named: bin_except.py PYTHON 3 ONLY

  • In the PowerPoint slides for lecture 15, you will find a function that performs binary search...

    In the PowerPoint slides for lecture 15, you will find a function that performs binary search on a Python list using recursion. Modify that binarySearchRec(alist, item) function so that it performs ternary search on a Python list. Your new function should find two midpoints that divide the list into three approximately equal size segments. If the item being searched for is found at either of the midpoints, the function should return True. If the item being searched for is less...

  • python: Write a function named "write_values" that takes a key-value store mapping strings to strings as...

    python: Write a function named "write_values" that takes a key-value store mapping strings to strings as a parameter and writes the values of the input to a file named "persuade.txt" with one element per line. If a file named "persuade.txt" already exists it must be overwritten. The function should not return any value

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...

  • All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary...

    All those points need to be in the code Project 3 Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort Class River describes river's name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles long and returns false otherwise. Class CTRivers describes collection of CT rivers. It has no data, and it provides the...

  • python 3 needs to read a local JSON file - it doesn't need to access the...

    python 3 needs to read a local JSON file - it doesn't need to access the internet. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as...

  • Write a program that write a value-returning function, isVowel, that returns the value true if a...

    Write a program that write a value-returning function, isVowel, that returns the value true if a given character is a vowel and otherwise returns false. You must insert the following comments at the beginning of your program and write our commands in the middle: Write a C++ Program: /* // Name: Your Name // ID: Your ID */ using namespace std; #include <iostream> using namespace std; bool isVowel(char ch); int main() {     char ch;     …     …         ...

  • Write a recursive function that returns the minimum key value in a binary search tree of...

    Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...

  • project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary,...

    project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary, and email_address (you must use those names - don't make them private). Write a function named make_employee_dict that takes as parameters a list of names, a list of ID numbers, a list of salaries and a list of email addresses (which are all the same length). The function should take the first value of each list and use them to create an Employee object....

  • Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int...

    Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int low, int high) { while (low <= high) { int mid = (low + high) / 2; if (a[mid] == target) return mid; else if (a[mid] < target) low = mid + 1; else high = mid 1: } return -1; } 1) If array a[] = {4, 5, 18, 25, 66, 70, 78}, size = 7, target = 71, low = 0, high...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT