w3resource

Python: Combine three numbers to get the target number

Python sets: Exercise-28 with Solution

Write a Python program to find all the unique combinations of 3 numbers from a given list of numbers, adding up to a target number.

Sample Solution:

Python Code:

# Define a function 'find_combinations_of_three' that takes a list of 'nums' and a 'target_val' as input.
def find_combinations_of_three(nums, target_val):
    # Convert the 'nums' list into a set to remove duplicates and then back to a list.
    nums = list(set(nums))
    
    # Create an empty set 'result' to store unique combinations of three numbers.
    result = set()
    
    # Iterate through the indices of 'nums' to consider pairs of numbers.
    for i in range(len(nums)):
        for j in range(i+1, len(nums)):
            # Calculate the third number ('complement') needed to reach the 'target_val'.
            complement = target_val - nums[i] - nums[j]
            
            # Check if the 'complement' exists in the remaining part of the list 'nums'.
            if complement in nums[:i] + nums[j+1:]:
                # Add a sorted tuple of the three numbers to the 'result' set.
                result.add(tuple(sorted((nums[i], nums[j], complement))))
    
    # Convert the 'result' set into a list and return it.
    return list(result)

# Define a list of numbers 'nums' and a 'target_val' for testing.
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
target_val = 12
print("Original list of numbers:")
print(nums)
print("Target value:", target_val)

# Call the 'find_combinations_of_three' function and print the result for the given list and target value.
print("Combine three numbers whose sum equals the target number:")
print(find_combinations_of_three(nums, target_val))

# Repeat the process for a different 'target_val'.
target_val = 17
print("\nOriginal list of numbers:")
print(nums)
print("Target value:", target_val)

# Call the 'find_combinations_of_three' function and print the result for the updated target value.
print("Combine three numbers whose sum equals the target number:")
print(find_combinations_of_three(nums, target_val)) 

Sample Output:

Original list of numbers:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Target value: 12
Combine three numbers whose sum equal to a target number:
[(1, 3, 8), (1, 4, 7), (1, 2, 9), (1, 5, 6), (3, 4, 5), (2, 3, 7), (2, 4, 6)]

Original list of numbers:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Target value: 17
Combine three numbers whose sum equal to a target number:
[(4, 5, 8), (2, 6, 9), (3, 5, 9), (2, 7, 8), (4, 6, 7), (3, 6, 8), (1, 7, 9)]

Flowchart:

Flowchart - Python Sets: Combine three numbers to get the target number.

Python Code Editor:

Previous: Find and group all the anagrams in the given list.
Next: Third largest number from a list of numbers using set.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/sets/python-sets-exercise-28.php