-3

I need a help with an algorithm that takes a list of integers and value and finds all lists that add up to value. Fore example : fn ([1,1,2,2,3],4) -> [[1,1,2],[1,3],[2,2]]

It must be an recursive soulution without added libraries. I have spent a lot of time figuring this out, but so far I have nothing.

Appreciate any help, thanks

LorNaP
  • 1
  • 2
    Generate all possible combinations of that list and check which of those combinations add up to the target value. – Selcuk Dec 01 '19 at 23:19
  • I would suggest you check out this [link](https://www.geeksforgeeks.org/print-combinations-of-distinct-numbers-which-add-up-to-give-sum-n/) in geeksforgeeks and read more about knapsack problem. – Ibrahim Sherif Dec 01 '19 at 23:32
  • 1
    What have you tried? Stack Overflow is not a place where people do all your work for you. See: [ask]. – AMC Dec 02 '19 at 00:11

1 Answers1

0

I need to have 50 reputation to comment above, so posting here.
Kindly do not consider this as an answer.

Generate all possible combinations of that list and check which of those combinations add up to the target value. As said by Selcuk above...


To generate all possible combinations of a list -

from itertools import combinations 
def mySubset(arr, r): 
    return list(combinations(arr, r)) 
if __name__ == "__main__": 
    arr = [1, 2, 3, 4] 
    r = 3
for x in arr:
  print(mySubset(arr, x))

It gives following result-

[(1,), (2,), (3,), (4,)]                                                                                                               
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]                                                                                       
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]                                                                                           
[(1, 2, 3, 4)] 
Kum
  • 333
  • 7
  • 20