# Sequential searches

Feb 12th, 2019
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. # sequential search on random list
2. # returns first position of item in list
3. # returns none if item is not in list
4. # Search2 improves efficiency on a sorted list by stopping once > passed item
5.
6. import random
7.
8. random_items = []
9.
10. def search(alist, item): #returns first position of item in a list
11.     for position, value in enumerate(alist,1):
12.         if value == item:
13.             return(value, position)
14.
15. def search_2(alist, item):
16.     for position, value in enumerate(alist,1):
17.         if item == value:
18.             return(value,position)
19.         elif item < value:
20.             return("item is not in list")
21.
22. # populate list
23. for x in range (0, 30):
24.     random_items.append(random.randint(0, 30))
25.
26. print(random_items)
27.
28. sorted_items = sorted(random_items)
29. print(sorted_items)
30.
31. print(search(random_items, 14))
32. print(search_2(sorted_items, 14))
RAW Paste Data