|
| 1 | +# Write a function that takes an ordered list of numbers |
| 2 | +# (a list where the elements are in order from smallest to |
| 3 | +# largest) and another number. The function decides whether |
| 4 | +# or not the given number is inside the list and returns |
| 5 | +# (then prints) an appropriate boolean. |
| 6 | + |
| 7 | + |
| 8 | +def Search(alist, item): |
| 9 | + first = 0 |
| 10 | + last = len(alist)-1 |
| 11 | + found = False |
| 12 | + while first <= last and not found: |
| 13 | + midpoint = (first + last)//2 |
| 14 | + if alist[midpoint] == item: |
| 15 | + found = True |
| 16 | + else: |
| 17 | + if item < alist[midpoint]: |
| 18 | + last = midpoint-1 |
| 19 | + else: |
| 20 | + first = midpoint+1 |
| 21 | + return found |
| 22 | + |
| 23 | + |
| 24 | +t = [0, 1, 2, 8, 13, 17, 19, 32, 42, ] |
| 25 | +print(Search(t, 3)) |
| 26 | +print(Search(t, 13)) |
0 commit comments