Link to home
Start Free TrialLog in
Avatar of beer9
beer9Flag for India

asked on

need to write a python function

need to write a python function that given an input array of integers with 4 entries and a desired
target_sum, returns the number of combinations, of any length, that add up to that target_sum

e.g.:

total_combinations = calculate_combinations([5, 5, 15, 10], target_sum=15)

Answer:
It should return 3, as there are 3 combinations of numbers from the input array that add up to 15, namely:

15
5+10
5+10
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr
pepr

Well, the comment was not meant to get points :) Thanks anyway.

Did you find the solution? The full answers are bad in the above sense; however, hints are allowed.

You should learn the standard module itertools. The brute-force solution can be based on the itertools.permutations function and on elimination of the same solutions. The standard documentation also shows how to implement it in Python. It can be a good start.