Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # literal search on unsorted and sorted list
- sequence = ['A','B','C','D']
- my_list = [2,4,1,6,5] # an unsorted list
- my_list_sorted = [1,2,3,5,6] # sorted list
- def whilesearch(alist, avalue):
- print("using whilesearch function")
- found = False
- pos = 0
- while pos < len(alist):
- if alist[pos] == avalue:
- print(alist[pos], "is at position", str(pos))
- found = True
- break
- else:
- pos += 1
- if not found:
- print(f"{avalue} not found")
- whilesearch(my_list, 6)
- def whilesearchsorted(alist, avalue):
- print("using whilesearchsorted function")
- found = False
- pos = 0
- while pos < len(alist) and not found:
- if alist[pos] > avalue:
- break
- elif alist[pos] == avalue:
- print(alist[pos], "is at position", str(pos))
- found = True
- break
- else:
- pos += 1
- if not found:
- print(f"{avalue} not found")
- whilesearchsorted(my_list_sorted, 4)
Advertisement
RAW Paste Data
Copied
Advertisement