[Tutor] Recursion

Glen Wheeler wheelege@tsn.cc
Wed, 6 Jun 2001 18:41:18 +1000


This is a multi-part message in MIME format.

------=_NextPart_000_0211_01C0EEB8.4655DFC0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

  Hey all,

  While fiddling around with maths, I thought it would be nice and cool =
to get all the possible permutations of a set, taken n at a time.  I =
have been trying to get this to work nicely, but I'm just not familiar =
enough with recursion to get it to behave.
  For example, if I wanted to get all the permutations of a set =
['a','b'] taken three at a time, I would write this :

>>> c =3D []
>>> s =3D ['a', 'b']
>>> for i1 in range(len(s)):
...  for i2 in range(len(s)):
...   for i3 in range(len(s)):
...    c.append([s[i1], s[i2], s[i3]])
...=20
>>> for item in c: print item
...=20
['a', 'a', 'a']
['a', 'a', 'b']
['a', 'b', 'a']
['a', 'b', 'b']
['b', 'a', 'a']
['b', 'a', 'b']
['b', 'b', 'a']
['b', 'b', 'b']

  Which is all well and good, but when I want to take them n at a time I =
need more for loops.  Thus I thought 'Hmmm, I could get a function which =
calls itself n times...that would work!' - but I'm just too green with =
this sort of programming for it to happen for me.
  Help?

  Thanks,
  Glen.

------=_NextPart_000_0211_01C0EEB8.4655DFC0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4522.1800" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV>&nbsp; Hey all,</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; While fiddling around with maths, I thought it would be nice =
and=20
cool to get all the possible permutations of a set, taken n at a =
time.&nbsp; I=20
have been trying to get this to work nicely, but I'm just not familiar =
enough=20
with recursion to get it to behave.</DIV>
<DIV>&nbsp; For example, if I wanted to get all the&nbsp;permutations of =
a set=20
['a','b'] taken three at a time, I would write this :</DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt;&gt;&gt; c =3D []<BR>&gt;&gt;&gt; s =3D ['a', =
'b']<BR>&gt;&gt;&gt; for i1=20
in range(len(s)):<BR>... &nbsp;for i2 in range(len(s)):<BR>... =
&nbsp;&nbsp;for=20
i3 in range(len(s)):<BR>... &nbsp;&nbsp;&nbsp;c.append([s[i1], s[i2],=20
s[i3]])<BR>... <BR>&gt;&gt;&gt; for item in c: print item<BR>... =
<BR>['a', 'a',=20
'a']<BR>['a', 'a', 'b']<BR>['a', 'b', 'a']<BR>['a', 'b', 'b']<BR>['b', =
'a',=20
'a']<BR>['b', 'a', 'b']<BR>['b', 'b', 'a']<BR>['b', 'b', 'b']<BR></DIV>
<DIV>&nbsp; Which is all well and good, but when I want to take them n =
at a time=20
I need more for loops.&nbsp; Thus I thought 'Hmmm, I could get a =
function which=20
calls itself n times...that would work!'&nbsp;- but I'm just too green =
with this=20
sort of programming for it to happen for me.</DIV>
<DIV>&nbsp; Help?</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; Thanks,</DIV>
<DIV>&nbsp; Glen.</DIV></BODY></HTML>

------=_NextPart_000_0211_01C0EEB8.4655DFC0--