Function
smallestDifference
(arr1 : list, arr2 : list)
Source from the content-addressed store, hash-verified
| 39 | """ |
| 40 | |
| 41 | def smallestDifference(arr1 : list, arr2 : list) : |
| 42 | arr1.sort();arr2.sort() |
| 43 | i = 0;j = 0 |
| 44 | |
| 45 | smallest = float("inf");current=float("inf") |
| 46 | smallestPair = list() |
| 47 | |
| 48 | while i < len(arr1) and j < len(arr2) : |
| 49 | fnum = arr1[i];snum = arr2[j] |
| 50 | |
| 51 | if fnum < snum : |
| 52 | current = snum - fnum |
| 53 | i += 1 |
| 54 | elif snum < fnum : |
| 55 | current = snum - fnum |
| 56 | j += 1 |
| 57 | else : |
| 58 | return [fnum, snum] |
| 59 | |
| 60 | if current < smallest : |
| 61 | smallest = current |
| 62 | smallestPair.append((fnum, snum)) |
| 63 | |
| 64 | |
| 65 | |
Tested by
no test coverage detected