From e9921debf53804f2bb5ce637e616a4cfc3f87c36 Mon Sep 17 00:00:00 2001 From: viktor Date: Wed, 2 Apr 2025 01:14:55 +0300 Subject: [PATCH] fix 12,22,12 edge case --- inputs/input_2_3_3_2.txt | 1 + nordhealth_test/solution.py | 11 +++++++++-- tests/test_solution.py | 7 +++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 inputs/input_2_3_3_2.txt diff --git a/inputs/input_2_3_3_2.txt b/inputs/input_2_3_3_2.txt new file mode 100644 index 0000000..350fa43 --- /dev/null +++ b/inputs/input_2_3_3_2.txt @@ -0,0 +1 @@ +2,3,3,2 \ No newline at end of file diff --git a/nordhealth_test/solution.py b/nordhealth_test/solution.py index 5f9aa1c..f45b72d 100755 --- a/nordhealth_test/solution.py +++ b/nordhealth_test/solution.py @@ -71,9 +71,16 @@ def print_pairs_with_same_sum(arr: List[int]) -> None: # Time Complexity (O(n^2)), as we have n(n-1)/2 for the second loop. for j in range(i + 1, n): current_sum = arr[i] + arr[j] - pair = tuple((arr[i], arr[j])) - if pair not in seen_pairs: + # In order to follow the provided output, I must not sort the values of the pair (eg. ( 12, 21) ( 22, 11) have sum : 33) + # however in case of [12, 21, 12] this will output (12,21) (21,12), since we have a second 12. If this is not + # to be considered unique pair, then we must reverse the tuple and check if it really is unique. + pair = tuple((arr[i], arr[j])) + reversed_pair = tuple((arr[j], arr[i])) + + # The check for reversed pairs. + if pair not in seen_pairs and reversed_pair not in seen_pairs: + # This will output nothing for [2,2,2,2]. Depending on the requirements we can store the pair using indices, # then the result would be: # (A[0],A[1]) (A[0],A[2]) (A[0],A[3]) (A[1],A[2]) (A[1],A[3]) (A[2],A[3]) - sum: 4 diff --git a/tests/test_solution.py b/tests/test_solution.py index 8b023fb..30ab1cb 100644 --- a/tests/test_solution.py +++ b/tests/test_solution.py @@ -41,6 +41,13 @@ def test_no_valid_pairs(): result = capture_output(print_pairs_with_same_sum, arr) assert result == expected_output +# Test case: Array with no unique pairs +def test_no_uniqe_pairs(): + arr = [2, 3, 3, 2] + expected_output = "" + result = capture_output(print_pairs_with_same_sum, arr) + assert result == expected_output + # Test case: Array with exactly one element def test_single_element(): arr = [10]