-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL00704.py
44 lines (40 loc) · 1.47 KB
/
L00704.py
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
from time import perf_counter_ns as ns
class Solution:
def search(self, nums: list[int], target: int) -> int:
low, high = 0, len(nums) - 1
while low <= high:
if (n := nums[mid := (low + high) // 2]) == target:
return mid
if n < target:
low = mid + 1
else:
high = mid - 1
return -1
if __name__ == '__main__':
ITERATIONS = 1_000
print(f'Running the basic tests {ITERATIONS:,} times...')
tests = (
([-1, 0, 3, 5, 9, 12], 9, 4),
([-1, 0, 3, 5, 9, 12], 2, -1),
([1], 1, 0),
([2, 5], 5, 1),
)
s = Solution()
for numbers, target, expected in tests:
print(f'search({numbers}, {target}) returned', end=' ')
if (result := s.search(numbers, target)) == expected:
print(f'the expected result {expected}', end=' ')
fastest = float('inf')
slowest = total = 0
for _ in range(ITERATIONS):
start = ns()
s.search(numbers, target)
end = ns()
time = end - start
fastest, slowest = min(time, fastest), max(time, slowest)
total += time
print(f'in an average of {total / ITERATIONS / 1e3:,.2f}μs '
f'(min: {fastest / 1e3:,.2f}μs, '
f'max: {slowest / 1e3:,.2f}μs)')
else:
print(f'a wrong result {result} (expected: {expected})')