1

I have a list of integers, they are randomly sorted and may repeat: mylist = [5,4,2,4,5,6,7,3,8,3] and a certain value (for example: value=35)

Now I want to get a list of list of integers out of mylist, we name it sumlist, that includes all the posible options of numbers that together add up to value.

So that when I would do:

sum=0
for i in  sumlist[0]:
   sum+=i

sum == value would return True.

Taxogatl
  • 21
  • 3
  • What do you want to do in a case where there are multiple options to get to the sum? I.e. [1,2,3,4,5,6] value=10 where I can 5+4+1 or 6+4 or 1+2+3+4 – Or Y Aug 19 '20 at 16:45
  • Does ```mylist``` always contain all of the numbers from 1 until len(mylist) and ordered? And please share your code attempting to solve the problem. – Shimon Cohen Aug 19 '20 at 16:46
  • @OrY I would want just one random option of the multiple options if this is possible. – Taxogatl Aug 19 '20 at 16:47
  • 1
    Use a recursive function. Loop through the elements of the list, subtract the element from the desired sum, then call the function recursively with the reduced sum and the other elements of the list. – Barmar Aug 19 '20 at 16:48
  • 1
    See https://stackoverflow.com/questions/4158988/algorithm-to-find-the-correct-set-of-numbers/4159096#4159096 – Barmar Aug 19 '20 at 16:50
  • If the list is as i asked before, i think you should loop through it from the end to the beginning taking the first number that fits in the wanted sum and deducting it from wanted sum. And continue that way. O(n) – Shimon Cohen Aug 19 '20 at 16:50
  • You could use [`itertools.permutations()`](https://docs.python.org/3/library/itertools.html#itertools.permutations) to get all the possible pairs of numbers in `mylist` and see which ones add up to the `value`. – martineau Aug 19 '20 at 16:58
  • @Barmar Why is the question closed as "needs more focus"? What's wrong with it? – VisioN Aug 19 '20 at 17:04
  • Because you've shown no attempt to solve the problem yourself, which we could then help you fix.' – Barmar Aug 19 '20 at 17:09
  • @Barmar "Needs more focus" states as _This question currently includes multiple questions in one. It should focus on one problem only._ – VisioN Aug 19 '20 at 17:13
  • @VisioN None of the standard close reasons specifically mention this in their canned text, but this is the one that is conventionally used. Think of it more as "you've asked us to do too much of your work because you didn't narrow the problem enough" – Barmar Aug 19 '20 at 17:25
  • Taxogatl: `permutations()` could also be used to find triplets, quadruplets, etc of the numbers. – martineau Aug 19 '20 at 17:38

1 Answers1

0

Itertools.combinations manages this quite easily. When you provide it with a list and a length, it will give all combinations possible of that length, and, unlike permutations, it removes duplicates. To ensure every option is tried, each length (from 0 until the full string) must be tried, as follows:

import itertools
def SumList(MyList,Value):
    for Length in range(1,len(MyList)):          
        for ListOfVals in itertools.combinations(MyList,Length): 
            Total=0
            for num in ListOfVals:
                Total+=num
            if Total==Value:
                
                return(ListOfVals)
                
mylist=[5,4,2,4,5,6,7,3,8,3]
value=35
print(SumList(mylist,value))



>>[5,4,5,6,7,8]

Just as a quick sidenote, this will always output the shortest combination, seeing as it iterates through the lengths from shortest to longest.

E-A
  • 179
  • 12
  • No need for `list` to loop over `itertools.combinations(MyList,Num)` and should be `for Num in range(1,len(MyList))` since `0` would not generate a sum. – dawg Aug 19 '20 at 21:42
  • 1
    Also, just style, consider not having names of `Num` and `num` in the same code. Confusing... But this is good! – dawg Aug 19 '20 at 21:49
  • Thanks for the suggestions! The list remained from when I was printing the whole thing out, to see how it worked, the nums thing was just not re-reading my code. Just out of interest though, is it considered bad practise to iterate through 0 as well? Its never really occurred to me to prevent it. – E-A Aug 20 '20 at 11:09
  • Bob: IMO, skipping zero would likely be a very minor optimization in this case. – martineau Aug 20 '20 at 17:40