-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathFind Closest Number to Zero.py
107 lines (92 loc) · 3.46 KB
/
Find Closest Number to Zero.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
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
101
102
103
104
105
106
107
class Solution:
def findClosestNumber1(self, nums: List[int]) -> int:
return min(nums, key=lambda x: (abs(x), -x))
def findClosestNumber2(self, nums: List[int]) -> int:
return min(nums, key=lambda x: abs(x - .1))
def findClosestNumber3(self, nums: List[int]) -> int:
return max((-abs(x), x) for x in nums)[1]
def findClosestNumber4(self, nums: List[int]) -> int:
return -min(zip(map(abs, nums), map(neg, nums)))[1]
def findClosestNumber5(self, nums: List[int]) -> int:
a = min(map(abs, nums))
return a if a in nums else -a
def findClosestNumber6(self, nums: List[int]) -> int:
a = abs(min(nums, key=abs))
return a if a in nums else -a
def findClosestNumber7(self, nums: List[int]) -> int:
x = min(nums, key=abs)
return x if x >= 0 or -x not in nums else -x
def findClosestNumber8(self, nums: List[int]) -> int:
return min(sorted(nums, reverse=True), key=abs)
def findClosestNumber9(self, nums: List[int]) -> int:
a = abs(nums[0])
for x in nums:
if x < 0:
x = -x
if x < a:
a = x
return a if a in nums else -a
def findClosestNumberA(self, nums: List[int]) -> int:
pos = 999999
neg = -pos
for x in nums:
if x < 0:
if x > neg:
neg = x
elif x < pos:
pos = x
return pos if pos <= -neg else neg
def findClosestNumberB(self, nums: List[int]) -> int:
pos = 999999
neg = -pos
for x in nums:
if x < pos and neg < x:
if x < 0:
neg = x
else:
pos = x
return pos if pos <= -neg else neg
def findClosestNumberC(self, nums: List[int]) -> int:
pos = 999999
neg = -pos
for x in nums:
if neg < x and x < pos:
if x < 0:
neg = x
else:
pos = x
return pos if pos <= -neg else neg
def findClosestNumberD(self, nums: List[int]) -> int:
pos = 999999
neg = -pos
for x in nums:
if neg < x < pos:
if x < 0:
neg = x
else:
pos = x
return pos if pos <= -neg else neg
def findClosestNumber(self, nums: List[int], timess=defaultdict(lambda: [0] * 10), testcase=[0]) -> int:
name = 'findClosestNumber'
solutions = [getattr(self, s)
for s in dir(self)
if s.startswith(name)
and s != name]
expect = dummy = object()
from time import perf_counter as time
for i in range(10):
shuffle(solutions)
for solution in solutions:
start = time()
result = solution(nums)
end = time()
if expect is dummy:
expect = result
assert result == expect
timess[solution.__name__][i] += end - start
testcase[0] += 1
if testcase[0] == 224:
for name, times in sorted(timess.items(), key=lambda nt: sorted(nt[1])):
print(name, *(f'{t*1e3:6.2f} ms' for t in sorted(times)[:3]))
return
return result