0

Here is my problem i need to generate all combination with different order possible for a size of X and the total sum must equal Y. I must give X and Y and i don't know X,Y by advance.

X and Y are integer only.

example :

X=4 #the size of the list must be equal to 4
Y=16 #the sum of the list must be equal to 16

[8,0,1,7] #the size is 4 and the sum of the number equal to 16 
[7,1,0,8] #the size is 4 and the sum of the number equal to 16 
[2,2,4,8] #the size is 4 and the sum of the number equal to 16
[1,1,6,8] #the size is 4 and the sum of the number equal to 16  

Edit :

My first solution would be to make a program write a python file with the combination of X / Y and for loop.

First i will calculate 0 to Y, then brute force it to found the sum of combination for Y. And finally generate all combination.

then execute or import the python file.

But it's not very elegant ...

Regards

Bussiere
  • 500
  • 13
  • 60
  • 119
  • 2
    what are the elements you allow in the list? in [0. 9]? what is the expected distribution of the result? – hiro protagonist May 10 '22 at 16:42
  • Does this answer your question? [Generate random numbers summing to a predefined value](https://stackoverflow.com/questions/3589214/generate-random-numbers-summing-to-a-predefined-value) – Sruthi May 10 '22 at 16:46
  • That's not a good duplicate, since it's sampling randomly, while here OP wants all combinations. – joanis May 10 '22 at 16:47
  • But @Bussiere, this feels like a homework assignment that you have not even tried to solve before coming here. Please show some effort first, and ask when you get stuck. – joanis May 10 '22 at 16:48
  • [How do I ask and answer homework questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – joanis May 10 '22 at 16:49
  • @joanis not it's for generating the law of probability for the condorcet random. – Bussiere May 10 '22 at 16:49
  • All right, good to know. – joanis May 10 '22 at 16:50
  • @joanis i've added my solution but it's meta programming and a little bit crude as a solution ... – Bussiere May 10 '22 at 16:51
  • Why would you not write a function that returns (or yields) all these combinations? I don't understand the value of meta-programming here. – joanis May 10 '22 at 16:52
  • It would be worth sharing your crude solution, it would help specify more precisely what you're trying to accomplish. – joanis May 10 '22 at 16:53
  • @joanis i have some difficulty using / understanding yield. That's why i prefer meta programming. – Bussiere May 10 '22 at 16:56
  • @hiroprotagonist i've edited the question, i should have precised that it's integer only. – Bussiere May 10 '22 at 16:57
  • Does this answer your question? [Generate all possible lists of length N that sum to S in Python](https://stackoverflow.com/questions/7748442/generate-all-possible-lists-of-length-n-that-sum-to-s-in-python) – SMMousaviSP May 10 '22 at 19:40
  • @Bussiere Here's a really good tutorial on generators (that's what yield creates): https://treyhunner.com/2018/06/how-to-make-an-iterator-in-python/ Spend the time to read and understand that page, you'll find it a worthwhile investment of your time. – joanis May 10 '22 at 20:31
  • And if you have a few hours on your hands, this is the online workshop I used to become quite proficient with all things iteration and generators in Python: https://pycon2018.trey.io/ (Note: No, I'm not affiliated with Trey Hunner, I just really like his material for learning Python.) Also well worth your time if you want to learn Python better. – joanis May 10 '22 at 20:35

0 Answers0