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...
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....
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.
Write a function sin_x_crossings(begin, end, skip) that uses sin math function which takes begin, end and...
Write a function sin_x_crossings(begin, end, skip) that uses sin math function which takes begin, end and skip as input parameters, and returns the x-points as a Python list that crosses the x-axis (i.e., x-value that just before it crosses). Hint: see axis-crossing lecture notes for hints. Note: you cannot use for or while loops. Note2: numpy has been imported as np For example: Test Result import math ans = sin_x_crossings(0, 4 * math.pi, 0.01) for val in ans: print(round(val, 2))...
void printList(double * A, int start, int end ) { if (start == end) //base case...
void printList(double * A, int start, int end ) { if (start == end) //base case { cout << A[start] << endl; } else //recursive case { cout << A[start] << endl; printList(A, start + 1, end); } } int main() { double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 }; printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6 cout << endl; //HELP HERE: Using a recursive method on C++,...
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the...
def read_words(filename, ignore='#'): """ Read a list of words ignoring any lines that start with the ignore character as well as any blank lines. """ return ['a', 'z'] How would I code this in Python?
Which list of elements below is present in all biomolecules in plants? Sulfur, silicon, and calcium...
Which list of elements below is present in all biomolecules in plants? Sulfur, silicon, and calcium Oxygen, hydrogen, and carbon Chlorine, iron, and zinc Phosphorus, nitrogen, and carbon Potassium, oxygen, and magnesium
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT