-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path35.py
54 lines (47 loc) · 1.4 KB
/
35.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
49
50
51
52
53
54
# [ LeetCode ] 35. Search Insert Position
def solution(nums: list[int], target: int) -> int:
start, end, answer = 0, len(nums)-1, 0
while start <= end:
middle = (start + end) // 2
if nums[middle] == target:
return middle
elif nums[middle] > target:
answer = middle
end = middle - 1
else:
answer = middle + 1
start = middle + 1
return answer
def another_solution(nums: list[int], target: int) -> int:
from bisect import bisect_left; return bisect_left(nums, target)
if __name__ == "__main__":
cases: list[dict[str, dict[str, list[int] | int] | int]] = [
{
"input": {
"nums": [1, 3, 5, 6],
"target": 5
},
"output": 2
},
{
"input": {
"nums": [1, 3, 5, 6],
"target": 2
},
"output": 1
},
{
"input": {
"nums": [1, 3, 5, 6],
"target": 7
},
"output": 4
}
]
for case in cases:
assert case["output"] == solution(
nums=case["input"]["nums"], target=case["input"]["target"]
)
assert case["output"] == another_solution(
nums=case["input"]["nums"], target=case["input"]["target"]
)