Finding Pair Sums with Target

  • Share this:

Code introduction


This function takes a list of numbers and a target sum, and returns a pair of numbers that add up to the target sum. If no such pair exists, it returns None.


Technology Stack : Set

Code Type : Function

Code Difficulty : Intermediate


                
                    
def a_sum_pairs(numbers, target_sum):
    seen = set()
    for number in numbers:
        complement = target_sum - number
        if complement in seen:
            return (complement, number)
        seen.add(number)
    return None                
              
Tags: