from itertools import permutations
def genere(nbval,total,order=True) :
"""
Generate all the tuples L of nbval positive or nul integer
such that sum(L)=total.
The tuples may be ordered (decreasing order) or not
"""
if nbval == 0 and total == 0 : yield tuple() ; raise StopIteration
if nbval == 1 : yield (total,) ; raise StopIteration
if total==0 : yield (0,)*nbval ; raise StopIteration
for start in range(total,0,-1) :
for qu in genere(nbval-1,total-start) :
if qu[0]<=start :
sol=(start,)+qu
if order : yield sol
else :
l=set()
for p in permutations(sol,len(sol)) :
if p not in l :
l.add(p)
yield p
if __name__=='__main__' :
print("How to obtain 5 by adding a+b+c (>=0) ? ")
print("Give me the list of (a,b,c) tuples.")
g=genere(3,5,order=False)
print(list(g))
print("How to obtain 6 by adding 3 positive or nul integers ?")
g=genere(3,6,order=True)
print(list(g))
Diff to Previous Revision
--- revision 1 2013-07-16 13:59:49
+++ revision 2 2013-07-16 14:01:28
@@ -2,7 +2,7 @@
def genere(nbval,total,order=True) :
"""
- Generate all the tuples L of n positive or nul integer
+ Generate all the tuples L of nbval positive or nul integer
such that sum(L)=total.
The tuples may be ordered (decreasing order) or not
"""