Search function


In [ ]:
#Search Pseudo-Code

#inputs list and value
#set counter to 0
#iterate through the list of numbers
#start a counter, while iterating through the list
#if the value is in the list, return the index generated with the counter
#else it is not in the list, return thast the value is not in the list
#if it is, return the current index

In [ ]:
#or: binary search

In [65]:
def finding(lst, value):
    index = 0
    for item in lst:
        if value == lst[index]:
            print("The value is in the list, and it's index is: " + str(index) + '.')
            #break
        if value not in lst:
            print("The value is not in the list.")
            break
        else:
            index += 1

In [66]:
import random
value1000 = random.randint(0,1000)

In [70]:
%time
lst10 = [random.randint(0,1000) for r in range(10)]
finding(lst, value1000)


CPU times: user 2 µs, sys: 1 µs, total: 3 µs
Wall time: 5.01 µs
The value is not in the list.

In [71]:
%time
lst100 = [random.randint(0,1000) for r in range(100)]
finding(lst100, value1000)


CPU times: user 2 µs, sys: 0 ns, total: 2 µs
Wall time: 5.01 µs
The value is not in the list.

In [72]:
%time
lst1000 = [random.randint(0,1000) for r in range(1000)]
finding(lst1000, value1000)


CPU times: user 2 µs, sys: 1e+03 ns, total: 3 µs
Wall time: 7.15 µs
The value is in the list, and it's index is: 452.