-
Notifications
You must be signed in to change notification settings - Fork 0
/
1920.py
executable file
·48 lines (36 loc) · 1.13 KB
/
1920.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
45
46
47
48
# -*- coding: utf-8 -*-
import unittest
def binary_search(sorted_list: list, first: int, last: int, target: int) -> int:
mid = int((first + last) / 2)
if first > last:
return 0
elif target == sorted_list[mid]:
return 1
else:
if target > sorted_list[mid]:
return binary_search(sorted_list, mid + 1, last, target)
else:
return binary_search(sorted_list, first, mid - 1, target)
def find_number(n: list, m: list) -> list:
result = []
n.sort()
for number in m:
result.append(binary_search(n, 0, len(n) - 1, number))
return result
class TestFindNumber(unittest.TestCase):
def test_find_number(self):
for n, m, expected in [
[
[4, 1, 5, 2, 3],
[1, 3, 7, 9, 3],
[1, 1, 0, 0, 1]
],
[
[1, 4, 99, 3, 2, 6, 7],
[5, 4, 3, 2, 1, 6, 7, 10, 20, 60, 70],
[0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
]
]:
self.assertEqual(expected, find_number(n, m))
if __name__ == '__main__':
unittest.main()