Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need help with this programming written in python Create a program that perfor

ID: 3887038 • Letter: I

Question

i need help with this programming written in python

Create a program that performs a binary search. Create a list with a random number of randomly generated items. Within the loops display in text the steps variables and values that occur. An example of what that output would look like is below:

enter a number between 1 and 30: 9
[3, 4, 5, 6, 11, 12, 13, 19, 20, 21, 23, 24]

comparing item 12 at index 5 to search item 9
comparing item 5 at index 2 to search item 9
comparing item 6 at index 3 to search item 9
comparing item 11 at index 4 to search item 9
Item 9 not found

Explanation / Answer

def binarysearch(x, hstack):

    low = 0

    uppervalue = len(hstack) - 1

    idy = int(uppervalue/2)

    foundval = hstack[idy] == x

    while not foundval:

        if lowervalue >= uppervalue:

            break

        if x > hstack[idy]:

            lowervalue = idy + 1

        else:

            uppervalue = idy - 1

        idy = int(.5 *(lowervalue + uppervalue))

        foundval = hstack[idy] == x

    if foundval:

        return idy # I have found the number!

    return False # the value is not present in the list

if '__main__' == __name__:

    # comment the input of your choice

    x = 9

    hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    #x = 12

    #hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    #x = 5

    #hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    #x = 6

    #hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    #x = 11

    #hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    print bsearch(x, hstack)

def binarysearch(x, hstack):

    low = 0

    uppervalue = len(hstack) - 1

    idy = int(uppervalue/2)

    foundval = hstack[idy] == x

    while not foundval:

        if lowervalue >= uppervalue:

            break

        if x > hstack[idy]:

            lowervalue = idy + 1

        else:

            uppervalue = idy - 1

        idy = int(.5 *(lowervalue + uppervalue))

        foundval = hstack[idy] == x

    if foundval:

        return idy # I have found the number!

    return False # the value is not present in the list

if '__main__' == __name__:

    # comment the input of your choice

    x = 9

    hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    #x = 12

    #hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    #x = 5

    #hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    #x = 6

    #hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    #x = 11

    #hstack = [3,4,5,6,11,12,13,19,20,21,23,24]

    print bsearch(x, hstack)