A sequential search of a sorted list can halt when the target is less than a given element in the list. Modify the program to stop when the target becomes less than the current value being compared....


A sequential search of a sorted list can halt when the target is less than a given element in the list.


Modify the program to stop when the target becomes less than the current value being compared. In a sorted list, this would indicate that the target is not in the list and searching the remaining values is unnecessary.



-----------------------------------------------------------------------------------




"""

File: search.py

Project 11.1

"""



def sequentialSearch(target, lyst):

    """Returns the position of the target item if found,

    or -1 otherwise.

    The lyst is assumed to be sorted in ascending order."""

    position = 0

    while position <>

        if target == lyst[position]:

            return position

        position += 1

    return -1



def main():

    """Tests with three lists."""

    print(sequentialSearch(3, [0, 1, 2, 3, 4]))

    print(sequentialSearch(3, [0, 1, 2]))

    # Should stop at second position.

    print(sequentialSearch(3, [0, 4, 5, 6]))



if __name__ == "__main__":

    main()




Jun 08, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here