[Tutor] Recursive combinations?
Andrew Wilkins
toodles@yifan.net
Wed, 15 Aug 2001 16:50:24 +0800
Hi all,
I'm trying to make combinations of 2 out of a set {1,2,3,...,n}
I can do it for a predefined set where n=10 using the following:
part1=[]
for a in range(1,11):
for b in range(a+1,11):
part1.append((a,b))
Is there some way I can make a recursive function so this combination
function can be extended where the amount of items in a combination could be
larger? For example for 3 items I'd just add another nested loop:
part1=[]
for a in range(1,11):
for b in range(a+1,11):
for c in range(b+1,11)
part1.append((a,b,c))
There's an obvious pattern, so that's why I thought of recursion. However I
can't seem to get my head around recursion, even after going through and
understanding Alan Gauld's tutorial on it.
(By the way, _no_ this isn't for homework *grin*)
Thanks,
Andrew Wilkins