Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Linear Search Sorted list
- from random import randint
- sequence = [randint(0,10) for i in range(30)]
- #sequence = ['a', 'C', 'B', 'A', 'c']
- sorted_sequence = sorted(sequence)
- print(sorted_sequence)
- def linear_search(sorted_sequence, value):
- for position, item in enumerate(sorted_sequence):
- if item == value:
- print(item, "is at position", position)
- return position
- #this causes program to stop once the desired item is found;
- #does not continue searching for more occurrences of desired item.
- elif item > value:
- print("We've already exceeded desired value {} with no success".format(value))
- return False
- elif position == len(sorted_sequence)-1:
- print(value, "is not in the sequence")
- return False
- #temp = linear_search(sorted_sequence, 'C')
- temp = linear_search(sorted_sequence, 7)
- print(temp)
- #temp = linear_search(sorted_sequence, 'F')
- temp = linear_search(sorted_sequence, 14)
- print(temp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement