Question

Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself, the sequence it passes on the recursive call should be a slice of the original sequence. To test the search functions, write function bintest, a new version of the test function you wrote for Project 6-1. Results will be similar to last time, for example: Checking isMemberR ((99, 100), 101).. .its value False is correct! Checking isMemberI ((2, 10), 10)... its value True is correct! Finally, write a main function that calls bintest two times: def main ) calls string reverse test func 2 times bintest (isMemberR) print(.) bintest (isMemberI) return None The last line in your py file should be a call to function main.

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

Given below is the code (tested on python 3.5) for the question.
************ EVERYTHING IS DONE. YOU JUST NEED TO COPY THE bintest() FROM YOUR PROJECT 6-1 AS MENTIONED IN QUESTION.. SINCE YOU HAVE NOT GIVEN THAT CODE, I HAVE JUST LEFT PLACE WITH COMMENT WHERE YOU NEED TO COPY THE bintest()*****************
Make sure to indent the code exactly as shown in image. INDENTATION IS LOST WHEN CODE IS UPLOADED ON CHEGG SITE
Please don't forget to rate the answer if it was helpful. Thank you
Let me know in case you need some help to integrate your previous code into this one.

#iterative function 2 def isMenberI(aseq, target): start 0 end en(aseq)-1 while start end: nid(start +end) // 2 if aseq [mid]target: return True end = mid-1 start nid1 elif target aseq [nid]: 12 13 return False 15 16 #recursive function 17 def isMenberR aseq, target) 18 19 20 21 if not aseq: return False míd = (len(aseq)-1) // 2 if targetaseq [nid]: return True 23 24 25 26 27 28 29 30 31 32 def bintest): elif target aseq [mid]: return isMemberR(aseq[8, mid], target) return isMemberR(aseq[mid+1:, target) 34 return 36 def main): 37 38 39 bintestisMemberR) print() bìntest( İsMember! } print() return None 41 43 main

#iterative function
def isMemberI(aseq, target):
start = 0
end = len(aseq) - 1
while start <= end:
mid = (start + end) // 2
if aseq[mid] == target:
return True
elif target < aseq[mid]:
end = mid - 1
else:
start = mid + 1

return False

#recursive function
def isMemberR(aseq, target):
if not aseq:
return False

mid = (len(aseq) - 1) // 2
if target == aseq[mid]:
return True
elif target < aseq[mid]:
return isMemberR(aseq[0, mid], target)
else:
return isMemberR(aseq[mid+1:], target)

#function bintest TO COPY FROM project 6
def bintest():
#TODO
return

def main():
bintest(isMemberR)
print()
bintest(isMemberI)
print()
return None

main()

Add a comment
Know the answer?
Add Answer to:
Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...
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 C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General...

    DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General requirement: Use cout to fully demonstrate your works for all tasks. The output must clearly show how each of these functions work. If no output is displayed for a task below, 0 point will be given to the task. If the output is not clear, some points may be deducted.    Task 1 – Reverse array (3 pts) Given an integer array like: const...

  • can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of...

    can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of two positive integers p and q can be given by the Euclid's algorithm explained in the lecture will be determined. · Write a function gcdIterative that uses the largest common divisor of p and q Calculates loop structure and returns. Use the pseudocode given in the lecture as a starting point and implement it as directly as possible into a C ++ program. Use...

  • Write a recursive function that takes a string as an input and returns the reverse of...

    Write a recursive function that takes a string as an input and returns the reverse of the string. Write a recursive function rec_string that produces the output shown below for the corresponding function calls. Write a main function to test the function. Method call rec_string(‘abcde’), will produce the following output: * e de cde bcde abcde Method call rec_string(‘abc’), will produce the following output: * c bc abc

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • Write a program that meets the following criteria: Define a function that implements the selection sort...

    Write a program that meets the following criteria: Define a function that implements the selection sort algorithm to sort an integer array. Define a function that implements the binary search algorithm to search an array for a given integer. The function must return the location of the integer if it is found or a -1 if it is not found. The main function in your program must declare an integer array initialized with 10 unsorted values. The main function must...

  • Implement the algorithm maxArray, discussed in Section 2.4.3, as a C++ function. Make sure the following...

    Implement the algorithm maxArray, discussed in Section 2.4.3, as a C++ function. Make sure the following requirements are met. Program must compile and run. maxArray function must be a recursive template function. Add a main function to test the maxArray function so that you have a complete program. Test the maxArray function on two arrays of different types. Name the program maxarray.cpp. Make sure the following requirements are met. 2.4.3 Finding the Largest Value in a Sorted or Unsorted Array...

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