-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson_1.py
More file actions
101 lines (78 loc) · 3.34 KB
/
Copy pathLesson_1.py
File metadata and controls
101 lines (78 loc) · 3.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'''
Lesson 1 - Binary Search, Linked Lists, and Complexities
Question:
Alice has some array with numbers written on them. She arranges the cards in decreasing order, and lays them out face down
in a sequence on a table. She challenges Bob to pick out the card containing a given number by turning over as few cards
as possible. Write a function to help Bob locate the card.
Interpretation of Question:
Given an array sorted in descending order, find the position of a target number.
Test Cases:
1. {'array': [13, 11, 10, 7, 4, 3, 1, 0], 'query': 7}
2. {'array': [13, 11, 10, 7, 4, 3, 1, 0], 'query': 1}
3. {'array': [4, 2, 1, -1], 'query': 4}
4. {'array': [3, -1, -9, -127], 'query': -127}
5. {'array': [6], 'query': 6}
6. {'array': [9, 7, 5, 2, -9], 'query': 4}
7. {'array': [], 'query': 7}
8. {'array': [8, 8, 6, 6, 6, 6, 6, 3, 2, 2, 2, 0, 0, 0], 'query': 3}
9. {'array': [8, 8, 6, 6, 6, 6, 6, 6, 3, 2, 2, 2, 0, 0, 0], 'query': 6}
'''
# Linear Search Solution [Time = O(N); Space = O(1)]
def linear_search(array, query):
position = 0
while position < len(array):
if array[position] == query:
return position
position += 1
return -1
# Binary Search Solution [Time = O(log(N)); Space = O(1)]
def binary_search_general(array, query): # General binary search algorithm
lo, hi = 0, len(array) - 1
while lo <= hi:
mid = (lo + hi) // 2
mid_num = array[mid]
if mid_num == query:
return mid
elif mid_num < query:
hi = mid - 1
elif mid_num > query:
lo = mid + 1
return -1
def binary_search_first(array, query): # Iterative use of binary search to find the first known occurance of the query
lo, hi = 0, len(array) - 1
ans = -1
while lo <= hi:
mid = (lo+hi) // 2
mid_num = array[mid]
if mid_num == query:
ans = mid
hi = mid - 1 # continuing the search but with the high index being one less than the queried index; so search continues with all numbers less than or equal to the query, resulting in us finding the first known occurance.
elif mid_num < query:
hi = mid - 1
elif mid_num > query:
lo = mid + 1
return ans
def binary_search_last(array, query): # Iterative use of binary search to find the last known occurance of the query
lo, hi = 0, len(array) - 1
ans = -1
while lo <= hi:
mid = (lo + hi) // 2
mid_num = array[mid]
if mid_num == query:
ans = mid
lo = mid + 1 # continuing the search but with the low index being one greater than the queried index; so search continues with all numbers greater than or equal to the query, resulting in us finding the last known occurance.
elif mid_num < query:
hi = mid + 1
elif mid_num > query:
lo = mid - 1
return ans
# Testing above search functions:
input = [8, 8, 6, 6, 6, 6, 6, 3, 2, 2, 2, 0, 0, 0]
query = 3
output = binary_search_first(input, query)
expected = 7
print("Input: {}".format(input))
print("Query: {}".format(query))
print("Output: {}".format(output))
if output != expected: print("~~~ FAIL - Output does not match Expected Output = {} ~~~".format(expected))
else: print("~~~ PASS - Output matches Expected Output = {} ~~~".format(expected))