Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- # this function will first sort a list and then apply the linear search by stopping at whenever item in list is bigger than value
- my_list2=[190, 496, 165, 371, 317, 307, 990, 303, 35, 598, 546, 969, 774, 418,
- 855, 956, 340, 578, 639, 417, 225, 741, 579, 227, 414, 112, 252, 254,
- 637, 295, 324, 408, 601, 831, 767, 340, 193, 38, 498, 95, 39, 690,
- 920, 321, 74, 80, 878, 728, 381, 28, 558, 477, 405, 311, 216, 820,
- 85, 95, 884, 425, 927, 471, 586, 690, 1000, 169, 899, 48, 661, 522,
- 130, 379, 948, 459, 999, 263, 62, 182, 663, 533, 84, 110, 360, 974,
- 95, 763, 728, 622, 500, 337, 693, 320, 864, 107, 77, 103, 776, 981,
- 625, 350]
- def linear_search_sortedlist(my_list2,value):
- my_list2_sorted=sorted(my_list2)
- print(my_list2_sorted)
- count=0
- position_list=[]
- for i in range(len(my_list2_sorted)):
- if my_list2_sorted[i ]<= value:
- #print("im here")
- if my_list2_sorted[i] == value:
- count+=1 # if item is several time there
- position_list.append(i)
- #print("item {} is at position(s) {} thus is found {} time(s)".format(value,i, count))
- else:
- print("from i= {} it's above {}. We stop".format(i,value))
- return False, position_list
- end,search3=linear_search_sortedlist(my_list2,95)
- value=95
- print("Position lists for {} is {}".format(value,search3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement