Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any 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