Question

In: Computer Science

#This function "binarySearchPythonCode" identifies the elements which is present in the start and end values def...

#This function "binarySearchPythonCode" identifies the elements which is present in the start and end values

def binarySearchPythonCode(list, Element, startElement, endElement):

#Variable which describes whether the element is present or not

isPresent = False

#condition check for start and end values

while startElement<=endElement and not isPresent:

#logic for calculating the mid value

midElement = startElement + endElement//2

# if condition which checks whether the mid value is same as element to be searched

if list[midElement] == Element:

#if condition satisfies it changes the flag to true

isPresent = True

#else condition which displays the end value and start value

else:

# if condition checks whether the element is present in the list or not

if Element < list[midElement]:

#if the element is present it calculates the end value

endElement = midElement-1

#else it will calculate the start value

else:

startElement = midElement+1

# returns the flag bit

return isPresent

list = [10, 20, 30, 40, 50, 60, 70, 80, 90]

print(binarySearchPythonCode(list, 40, 10, 90))

python code gives error, index error, how to fix?

Solutions

Expert Solution

Problem With Code:

=====================

=> Binary search logic is based on index but you are passing start and end elements instead of their indexes.

=> The function is recursive but in current code it calls only once. It is not called recursively after updating firstElement index and endElement.

Updated Solution:

=====================

def binarySearchPythonCode(list, Element, startElement, endElement):

#Variable which describes whether the element is present or not
isPresent = False
#condition check for start and end values
while startElement<=endElement and not isPresent:
#logic for calculating the mid value
midElement = (startElement + endElement) // 2
# if condition which checks whether the mid value is same as element to be searched
if list[midElement] == Element:
#if condition satisfies it changes the flag to true
isPresent = True
#else condition which displays the end value and start value
else:
# if condition checks whether the element is present in the list or not
if Element < list[midElement]:
#if the element is present it calculates the end value
endElement = midElement-1
binarySearchPythonCode(list, Element, startElement, endElement)
#else it will calculate the start value
else:
startElement = midElement+1
binarySearchPythonCode(list, Element, startElement, endElement)
# returns the flag bit
return isPresent

list = [10, 20, 30, 40, 50, 60, 70, 80, 90]

print(binarySearchPythonCode(list, 40, 0, len(list)-1))

Description:

==============

=> The above solution solves the problems that I have mentioned in the beginning.

=> The search for particular element in list using binary search recursively.  


Related Solutions

Which of the following are steps in the process of determining which elements are present in...
Which of the following are steps in the process of determining which elements are present in stars? (Select all that apply.) Plot the spectra of stars and measure the wavelengths at which absorption lines occur. Collect a sample of gas from a star. Match features in the spectra to known atomic lines of different elements. Identify the wavelength with the maximum intensity in the star's spectrum. Which of the following statements best completes the sentence: "When an atom absorbs a...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of consecutive integers, from start to end. If start > end then an empty list is generated. For example: (gen-list 1 5) ---> (1 2 3 4 5) b) write a recursive function pair-sum? that takes an integer sequence as generated by the gen-list function in exercise 4 above. This function tests whether any two adjacent values in the given list sum to the given...
Javascript 1. Write a function which receives four arguments (array, start, end, delimiter). Copy the array...
Javascript 1. Write a function which receives four arguments (array, start, end, delimiter). Copy the array values from the start to end index separated by delimiter\'s value. function([1,2,3,4],1,3,'*') // returns 2*3*4 as string 2. Write a function to change the case of all the characters in string based on their position which matches the value of the pos parameter passed to function(str, pos [even|odd]). Example: function(‘abCd’, ‘odd’) // returns Abcd 3 Write a function which receives two arguments (array, oddOrEven)...
1. A _________ schedule identifies major types of activities and approximate start and end dates for use in decision making about the scope of the project.
Chapter  8  Project Time Management ·         Types of Schedules ·         Elements of Time Management ·         Critical Path and Float ·         Managing the Schedule ·         Project Scheduling Software 1. A _________ schedule identifies major types of activities and approximate start and end dates for use in decision making about the scope of the project. 2. A schedule of activities that is prepared every two weeks is the _______ schedule. 3. How does a conceptual schedule differ from a master schedule? 4....
Please use python3 Create the function team_average which has the following header def team_average(filename): This function...
Please use python3 Create the function team_average which has the following header def team_average(filename): This function will return the average number of games won by the Red Sox from a text file with entries like this 2011-07-02 Red Sox @ Astros Win 7-5 2011-07-03 Red Sox @ Astros Win 2-1 2011-07-04 Red Sox vs Blue Jays Loss 7-9 This function should create a file object for the file whose name is given by the parameter filename. If the file cannot...
Write a C++ function which will search for TWO elements. The two elements must be right...
Write a C++ function which will search for TWO elements. The two elements must be right next to each other, contiguously. The prototype of the function should be: bool search_adjacent_elements(int const *x, size_t n, int target1, target2); It will return true if target1 and target2 exist in the array x somewhere, with target1 immediately to the left of target2. For example: int x[] = { 3, 5, 10, 14, 19, 25, 45, 60 }; cout << search_adjacent_elements(x, sizeof x /...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
This function will receive a list of elements with duplicate elements, this function should remove the...
This function will receive a list of elements with duplicate elements, this function should remove the duplicate elements in the list and return a list without duplicate elements. The elements in the returned list must be in the same order that they were found in the list the function received. A duplicate element is an element found more than one time in the specified list. JAVA
Which description best identifies the unique attributes of connective tissue? Which description best identifies the unique...
Which description best identifies the unique attributes of connective tissue? Which description best identifies the unique attributes of connective tissue? A. Connective tissues, unlike other tissues, lack blood vessels and blood. B. Connective tissues transmit signals from one part of the body to another. C. Connective tissue is characterized by an extracellular matrix. D. Connective tissues are hard and inflexible, whereas soft, flexible structures of the body are made up of other tissue types.
import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(")...
import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(") " + entry) return int(input(question)) plz explain this code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT