How to build lists where there are y items in the list, and the sum of the items are x? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to build lists where there are y items in the list, and the sum of the items are x?

Say the user input is 3 and 4. I'm trying to write a code that will (in this example) give me three lists with 4 items each, where the sum of the items of any given list equals three. So in this case, I would get [3,0,0,0] [2,1,0,0] [1,1,1,0] BTW, These lists will evenutually get permutated. Would it be easier to direclty generate all of the permutations at once instead of making one permutation and then permuting it? Ex [3,0,0,0] [0,3,0,0] [0,0,3,0] etc. (And do the same with the other lists)

28th May 2018, 12:54 PM
Jordan Pagé
Jordan Pagé - avatar
1 Answer
+ 3
You could try a two-dimensional array list with linear sorting if your adding more info.
28th May 2018, 5:37 PM
Apple Blossom
Apple Blossom - avatar