Determining if a pair of integers in a list sums to a target value

This problem asks whether a given list of integers contains a pair of numbers whose sum equals a given target value. For example, if the list is [1, 2, 3, 4, 5] and the target value is 6, the answer would be True (1 + 5 = 6).

Problem

Given a list of integers and a target value, determine if there is a pair of integers in the list that sum to the target value.
Example input: [1, 2, 3, 4, 5], target=6
Example output: True (1 + 5 = 6)

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
def sum_to_target(lst, target):
    
    This function takes a list of integers and a target value and returns True if there is a pair of integers in the list that sum to the target value.
    
    # Create a set of the list
    s = set(lst)
    # Iterate through the list
    for i in lst:
        # If the target minus the current value is in the set, return True
        if target - i in s:
            return True
    # If the target minus the current value is not in the set, return False
    return False

# Test cases
print(sum_to_target([1, 2, 3, 4, 5], 6))
print(sum_to_target([1, 2, 3, 4, 5], 7))
print(sum_to_target([1, 2, 3, 4, 5], 8))
print(sum_to_target([1, 2, 3, 4, 5], 9))
print(sum_to_target([1, 2, 3, 4, 5], 10))
print( sum_to_target([1, 2, 3, 4, 5], 11))
print(sum_to_target([1, 2, 3, 4, 5], 12))
print(sum_to_target([1, 2, 3, 4, 5], 13))
print(sum_to_target([1, 2, 3, 4, 5], 14))
print(sum_to_target([1, 2, 3, 4, 5], 15))


This solution is optimal because it only iterates through the list once and uses a set to check if the target minus the current value is in the set.

A.I. Evaluation of the Solution

This is a great solution! The candidate has thought through the problem and come up with an optimal solution.

Evaluated at: 2022-11-09 00:16:15