Last active
April 25, 2025 21:55
-
-
Save DonExo/4b637b7fa23c410c5fbe084775e3a6a5 to your computer and use it in GitHub Desktop.
NordHealth assignment
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 characters
from collections import defaultdict | |
def generate_pairs(array): | |
""" | |
Generates all unique pairs of elements from the input array and groups them by their sum. | |
Args: | |
array (list): A list of integers (or any elements that support addition). | |
Returns: | |
defaultdict: A dictionary where the keys are sums of pairs, and the values are sets | |
of tuples representing the pairs that add up to the corresponding sum. | |
Each pair is stored as a sorted tuple to avoid duplicate pairs in | |
different orders. | |
""" | |
sum_pairs = defaultdict(set) | |
array_length = len(array) | |
for i in range(array_length): | |
for j in range(i + 1, array_length): | |
number_i, number_j = array[i], array[j] | |
current_sum = number_i + number_j | |
# Store the pair in sorted order to avoid considering (a, b) and (b, a) as different pairs | |
pair = tuple(sorted((number_i, number_j))) | |
sum_pairs[current_sum].add(pair) | |
return sum_pairs | |
def clean_array_from_non_numeric_items(array): | |
"""Cleansing all non-numeric elements from the input array.""" | |
cleaned_array = [item for item in array if isinstance(item, (int, float))] | |
return cleaned_array | |
def print_unique_pairs(array): | |
""" | |
Prints all unique pairs of elements from the input array that have the same sum. | |
Args: | |
array (list): A list of integers (or any elements that support addition). | |
Returns: | |
None: This function does not return anything. It prints the results directly in the desired format. | |
""" | |
cleaned_array = clean_array_from_non_numeric_items(array) | |
if len(cleaned_array) < 2: | |
print("Not enough numeric elements to form pairs.") | |
return | |
sum_pairs = generate_pairs(cleaned_array) | |
# Collect sums that have at least two pairs and sort them | |
valid_sums = [key for key in sum_pairs if len(sum_pairs[key]) >= 2] | |
valid_sums.sort() # [16, 32, 33, 43, 53, 54, 64] | |
if not valid_sums: | |
print("No sum is associated with at least two unique pairs.") | |
return | |
for sum_value in valid_sums: | |
pairs = sum_pairs[sum_value] | |
print(f"Pairs :", end=" ") | |
for pair in pairs: | |
print(f"({pair[0]}, {pair[1]})", end=" ") | |
print(f"have sum : {sum_value}") | |
# More python way of achieving the same, but less readable | |
# print(f"Pairs : {' '.join(f'({a}, {b})' for a, b in pairs)} have sum : {sum_val}") | |
if __name__ == '__main__': | |
test_cases = [ | |
[6, 4, 12, 10, 22, 54, 32, 42, 21, 11], # requirement sample 1 | |
[4, 23, 65, 67, 24, 12, 86], # requirement sample 2 | |
[], # Empty array | |
[5], # Single element | |
[2, 2, 2, 2], # All identical elements | |
[-1, -2, 1, 2], # Negative numbers | |
[1, 2, 3, 4, "f"], # discard incorrect element in the array | |
[1, 1, 2, 2], # Duplicate pairs | |
[1.5, 2.5, 3.5, 4.5], # Floating-point numbers | |
[1000000, 2000000, 3000000, 4000000], # Large numbers | |
] | |
for i, arr in enumerate(test_cases, 1): | |
print(f"\nTest Case {i}: Input = {arr}") | |
print_unique_pairs(arr) | |
# Output | |
Test Case 1: Input = [6, 4, 12, 10, 22, 54, 32, 42, 21, 11] | |
Pairs : (4, 12) (6, 10) have sum : 16 | |
Pairs : (10, 22) (11, 21) have sum : 32 | |
Pairs : (12, 21) (11, 22) have sum : 33 | |
Pairs : (11, 32) (21, 22) have sum : 43 | |
Pairs : (21, 32) (11, 42) have sum : 53 | |
Pairs : (22, 32) (12, 42) have sum : 54 | |
Pairs : (10, 54) (22, 42) have sum : 64 | |
Test Case 2: Input = [4, 23, 65, 67, 24, 12, 86] | |
Pairs : (23, 67) (4, 86) have sum : 90 | |
Test Case 3: Input = [] | |
Not enough numeric elements to form pairs. | |
Test Case 4: Input = [5] | |
Not enough numeric elements to form pairs. | |
Test Case 5: Input = [2, 2, 2, 2] | |
No sum is associated with at least two unique pairs. | |
Test Case 6: Input = [-1, -2, 1, 2] | |
Pairs : (-1, 1) (-2, 2) have sum : 0 | |
Test Case 7: Input = [1, 2, 3, 4, 'f'] | |
Pairs : (2, 3) (1, 4) have sum : 5 | |
Test Case 8: Input = [1, 1, 2, 2] | |
No sum is associated with at least two unique pairs. | |
Test Case 9: Input = [1.5, 2.5, 3.5, 4.5] | |
Pairs : (1.5, 4.5) (2.5, 3.5) have sum : 6.0 | |
Test Case 10: Input = [1000000, 2000000, 3000000, 4000000] | |
Pairs : (2000000, 3000000) (1000000, 4000000) have sum : 5000000 | |
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 characters
Given an unsorted array A[]. The task is to print all unique pairs in the unsorted array with equal sum. | |
Note: Print the result in the format as shown in the below examples. | |
Examples: | |
Input: A[] = { 6, 4, 12, 10, 22, 54, 32, 42, 21, 11} | |
Output: | |
Pairs : ( 4, 12) ( 6, 10) have sum : 16 | |
Pairs : ( 10, 22) ( 21, 11) have sum : 32 | |
Pairs : ( 12, 21) ( 22, 11) have sum : 33 | |
Pairs : ( 22, 21) ( 32, 11) have sum : 43 | |
Pairs : ( 32, 21) ( 42, 11) have sum : 53 | |
Pairs : ( 12, 42) ( 22, 32) have sum : 54 | |
Pairs : ( 10, 54) ( 22, 42) have sum : 64 | |
Input:A[]= { 4, 23, 65, 67, 24, 12, 86} | |
Output: | |
Pairs : ( 4, 86) ( 23, 67) have sum : 90 |
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 characters
from assignment import generate_pairs, clean_array_from_non_numeric_items | |
def test_generate_pairs_input_1(): | |
# Test with requirement input 1 | |
array = [6, 4, 12, 10, 22, 54, 32, 42, 21, 11] | |
result = generate_pairs(array) | |
# For example, sum 16 should be formed by (6,10) and (4,12) | |
expected_sum = 16 | |
expected_pairs = {(6, 10), (4, 12)} | |
assert result[expected_sum] == expected_pairs, ( | |
f"Expected pairs for sum {expected_sum}: {expected_pairs}, got {result[expected_sum]}" | |
) | |
expected_sum = 54 | |
expected_pairs = {(22, 32), (12, 42)} | |
assert result[expected_sum] == expected_pairs, ( | |
f"Expected pairs for sum {expected_sum}: {expected_pairs}, got {result[expected_sum]}" | |
) | |
def test_generate_pairs_input_2(): | |
# Test with requirement input 2 | |
array = [4, 23, 65, 67, 24, 12, 86] | |
result = generate_pairs(array) | |
expected_sum = 90 | |
expected_pairs = {(23, 67), (4, 86)} | |
assert result[expected_sum] == expected_pairs, ( | |
f"Expected pairs for sum {expected_sum}: {expected_pairs}, got {result[expected_sum]}" | |
) | |
def test_clean_array_from_non_numeric_items(): | |
array = [6, 4, 12, 10, 22, 54, 32, 42, 21, 11, "f"] | |
result = clean_array_from_non_numeric_items(array) | |
assert result == [6, 4, 12, 10, 22, 54, 32, 42, 21, 11] | |
if __name__ == '__main__': | |
test_generate_pairs_input_1() | |
test_generate_pairs_input_2() | |
test_clean_array_from_non_numeric_items() | |
print("All tests passed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment