Created
December 1, 2020 18:16
-
-
Save willy-r/a348f3fce9ea48d17c1615ee60e14600 to your computer and use it in GitHub Desktop.
Revisions
-
willy-r created this gist
Dec 1, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ def array_diff(arr: list[int], n: int) -> int: count = 0 for i in range(len(arr)): for j in range(i + 1, len(arr)): if arr[i] - arr[j] == n or arr[j] - arr[i] == n: count += 1 return count def test_example(): assert array_diff([1, 2, 3, 4], 1) == 3 def test_positive_values(): assert array_diff([2, 4, 6, 8, 10], 2) == 4 def test_negative_values(): assert array_diff([-1, -2, -3, -4], -1) == 3 def test_target_zero_with_duplicates(): assert array_diff([1, 1, 2, 3, 3, 4], 0) == 2 def test_negative_target(): assert array_diff([5, 1, 3, 4, 2], -1) == 4 if __name__ == '__main__': test_example() test_positive_values() test_negative_values() test_target_zero_with_duplicates() test_negative_target()