Facebook

Course Name Start Date Time Duration Registration Link
Python for Testers 15th July 2020 9.00 PM IST to 10:30 PM IST 10 days ClickHere
Please mail To sudhakar@qtpsudhakar.com to Register for training

Tuesday, June 16, 2020

Find combination of numbers whose sum equal to specific value


def getCombinations(arr, exp, somepart=[]):
    total = sum(somepart)
    if total == exp:
        print(somepart)
    if total >= exp:
        return
    for i in range(0, len(arr)):
        getCombinations(arr[i + 1:], exp, somepart + [arr[i]])


getCombinations([2, 4, 5, 8, 12, 17, 21, 3], 20)

No comments :

Post a Comment