- Added a more generic tuplesdo.py

This commit is contained in:
ccremers
2005-01-20 15:47:23 +00:00
parent 094267cd03
commit c354401d16
4 changed files with 56 additions and 25 deletions

21
test/tuplesdo.py Executable file
View File

@@ -0,0 +1,21 @@
# Tuple module
#
# tuplesDo generates all unordered sets (in a list) of size n of the
# elements of the list l. The resulting lists (of length n) are passed
# to the function f.
def tuplesDo (f,l,n):
def tuplesDoRecurse (l,r):
if r and (len(r) == n):
f(r)
else:
if l and (n > 0):
# Larger size: we have options
# Option 1: include first
tuplesDoRecurse (l[1:], r + [l[0]])
# Option 2: exclude first
tuplesDoRecurse (l[1:], r)
tuplesDoRecurse (l,[])