Target Sum

Two Sum

from typing import List, Literal


class TwoSum:
    """Find all the possible combinations of two numbers
    in a list that sum up to a target integer.

    Example:
        Input: nums = [2, 7, 11, 15], target = 9
        Output: [[2, 7]]
    """
    def __init__(self, method: Literal["brute_force"]) -> None:
        self.method = method

    def __call__(self, nums: List, target: int) -> List:
        """
        :param nums: list of integers
        :param target: target integer
        """
        if self.method == "brute_force":
            return self._brute_force_two_sum(nums, target)

    def _brute_force_two_sum(self, nums: List, target: int) -> List:
        """Find all the possible combinations of two numbers
        in a list that sum up to a target integer.

        :param nums: list of integers
        :param target: target integer
        """
        nums_sorted = sorted(nums)
        result = []
        length = len(nums_sorted)
        for i in range(length):
            for j in range(i+1, length):
                combination = [nums_sorted[i], nums_sorted[j]]
                if sum(combination) == target and combination not in result:
                    result.append(combination)

        return result

Planted: by ;

L Ma (2024). 'Target Sum', Datumorphism, 01 April. Available at: https://datumorphism.leima.is/wiki/algorithms/practices/target-sum/.