'How to implement an algorithm for generating tuples of arbitrary size with sum of elements = N in python [duplicate]
I am trying to implement a generator function generate_tuples which yields every possible combination of numbers with a given sum.
Given a size, elements_sum so that
generate_tuples(size=3, elements_sum=4)
it would yield these tuples:
(0, 0, 4)
(0, 1, 3)
(0, 2, 2)
(0, 3, 1)
(0, 4, 0)
(1, 0, 3)
(1, 1, 2)
(1, 2, 1)
(1, 3, 0)
(2, 0, 2)
(2, 1, 1)
(2, 2, 0)
(3, 0, 1)
(3, 1, 0)
(4, 0, 0)
For now I ended up with multiple generators for each size. For instance:
def generate_tuples3(elements_sum: int):
for x in range(elements_sum + 1):
for y in range(elements_sum + 1):
for z in range(elements_sum + 1):
if sum([x, y, z]) != elements_sum:
continue
yield x, y, z
This works, but it's very inefficient and includes a lot of code duplication.
Solution 1:[1]
Thanks to comment posted by Tim Peters, I was able to find this algorithm which is exactly what I need!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Uberbaza |
