Skip to content
Advertisement

Find three numbers from a list that add up to a specific number

How can I adjust the below code to find three numbers instead of two numbers yielding the target amount? I’ve already came across the code below from another question titled “Find two numbers from a list that add up to a specific number”. Thank you.

JavaScript

Advertisement

Answer

If the numbers list is relatively small, you can use the itertools combinations function from the Python standard library.

JavaScript

Which gives the result:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement