Finding Pair Sums with Target

  • Share this:

Code introduction


This function accepts a list of numbers and a target sum, and returns a tuple containing two numbers whose sum equals the target sum. If no such pair is found, it returns None.


Technology Stack : List (list), Dictionary (dict)

Code Type : Function

Code Difficulty : Intermediate


                
                    
def a_sum_pairs(numbers, target_sum):
    seen = {}
    for number in numbers:
        complement = target_sum - number
        if complement in seen:
            return (complement, number)
        seen[number] = True
    return None