-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmallestDifference.py
More file actions
36 lines (32 loc) · 875 Bytes
/
smallestDifference.py
File metadata and controls
36 lines (32 loc) · 875 Bytes
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
import math
def smallestDifference(arrayOne, arrayTwo):
# Write your code here.
arrayOne = sorted(arrayOne)
arrayTwo = sorted(arrayTwo)
smallestSoFar = math.inf
result = []
i = 0
j = 0
while i < len(arrayOne) - 1 and j < len(arrayTwo) - 1:
e1 = arrayOne[i]
e2 = arrayTwo[j]
print([e1, e2], i , j)
currDiff = math.fabs(e1 - e2)
if currDiff < smallestSoFar:
smallestSoFar = currDiff
result = [e1, e2]
if e1 < e2:
i += 1
elif e1 > e2:
j += 1
while i < len(arrayOne) or j < len(arrayTwo):
currDiff = math.fabs(arrayOne[i] - arrayTwo[j])
if currDiff < smallestSoFar:
smallestSoFar = currDiff
result = [arrayOne[i], arrayTwo[j]]
if i == len(arrayOne) - 1:
j += 1
elif j == len(arrayTwo) -1:
i += 1
return result
print(smallestDifference([0, 20], [21, -2]))