Smallest Difference Code Structure

J. David Martinez

J. David Martinez

Open DS&A Founder

Here, I think the smallest difference code is more readable but, it might not be following best practices:

def smallestDifference(arrayOne, arrayTwo):
arrayOne.sort()
arrayTwo.sort()
ptrOne = 0
ptrTwo = 0
smallest = float("inf")
current_difference = float("inf")
while ptrOne < len(arrayOne) and ptrTwo < len(arrayTwo):
x = arrayOne[ptrOne]
y = arrayTwo[ptrTwo]
if x < y:
current_difference = y - x
ptrOne += 1
elif y < x:
current_difference = x - y
ptrTwo += 1
else:
return [x, y]
if current_difference < smallest:
smallest = current_difference
smallest_pair = [x, y]
return smallest_pair

This is just my honest opinion 🙂.