-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20_ElementSearch.py
More file actions
55 lines (42 loc) · 1.34 KB
/
Copy path20_ElementSearch.py
File metadata and controls
55 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Exercise 20: Element Search
Level: X
Write a function that takes an ordered list of numbers (a list where the elements are in order from smallest to largest) and another number. The function decides whether or not the given number is inside the list and returns (then prints) an appropriate boolean.
Extras:
- Use binary search
"""
import random
def binarySearch(arr, num):
print(arr)
size = len(arr)
print(size)
element = arr[0]
isFound = False
if size > 1:
while isFound == False:
element = arr[int(size/2)]
# if size == 0:
# isFound = False
# break
print("Evaluate: {} and {}".format(num,element))
if num == element:
isFound = True
print("BREAK")
break
elif num < element:
arr = arr[:int(size/2)]
binarySearch(arr, num)
break
elif num > element:
arr = arr[int(size/2)+1:]
binarySearch(arr, num)
break
# isFound = True
if size == 1 and num == arr[0]:
isFound = True
return isFound
arr = random.sample(range(0, 20), 15)
arr.sort()
# arr = [1, 3, 5, 30, 42, 43, 500]
num = int(input("Enter number to search: "))
print(binarySearch(arr, num))