-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
70 lines (61 loc) · 1.7 KB
/
search.py
File metadata and controls
70 lines (61 loc) · 1.7 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def sequential_search_itr(xs, y):
'''
Check whether y is contained in the list xs
>>> sequential_search_itr([1, 3, 5, 4, 2, 0], 2)
True
>>> sequential_search_itr([1, 3, 5, 4, 2, 0], 6)
False
'''
for x in xs:
if x == y:
return True
return False
def binary_search(xs, y):
'''
Assume that xs is a list of numbers sorted from LOWEST to HIGHEST.
Return True if y is in the list xs.
>>> binary_search([1, 3, 5, 7, 9, 11], 9)
True
>>> binary_search([1, 3, 5, 7, 9, 11], 8)
False
>>> binary_search(list(range(-1001, 1001, 2)), 9)
True
>>> binary_search(list(range(-1000, 1000, 2)), 9)
False
'''
if len(xs) == 0:
return False
mid = len(xs) // 2
if xs[mid] > y:
return binary_search(xs[:mid], y)
if xs[mid] < y:
return binary_search(xs[mid+1:], y)
if xs[mid] == y:
return True
def trinary_search(xs, y):
'''
Assume that xs is a list of numbers sorted from LOWEST to HIGHEST.
Return True if y is in the list xs.
>>> trinary_search([1, 3, 5, 7, 9, 11], 9)
True
>>> trinary_search([1, 3, 5, 7, 9, 11], 8)
False
>>> trinary_search(list(range(-1001, 1001, 2)), 9)
True
>>> trinary_search(list(range(-1000, 1000, 2)), 9)
False
'''
if len(xs) == 0:
return False
if len(xs) == 1:
return xs[0] == y
mid1 = len(xs) // 3
mid2 = 2 * len(xs) // 3
if y == xs[mid1] or y == xs[mid2]:
return True
if y < xs[mid1]:
return trinary_search(xs[:mid1], y)
if xs[mid1] < y < xs[mid2]:
return trinary_search(xs[mid1:mid2], y)
if xs[mid2] < y:
return trinary_search(xs[mid2:], y)