[Tutor] Recursive permutation

Blake Winton bwinton at latte.ca
Thu Nov 18 16:16:12 CET 2004


Eivind Triel wrote:
 > Well is there a nice code that will do the trick?

We've discussed this on the list before, so searching the archive for 
"list permutation recursive" should yield up some hits.  (Sadly, it 
doesn't really give me anything.  Maybe someone else on the Tutor list 
will post the thread title?)

> This code maks a permutation of the entire list (all the 3 digs), but 
> how do I make the permutations of lower lists?
> Going one down i seems easy: Remove alle the element one at a time and 
> make a permultation of the rest. But going two down...

Perhaps it would be easier to start from the bottom, and build your way 
up?  So, all the lists of length 0, then all the lists of length 1, etc, 
etc, until you get to all the lists of length len(input).

> Like this:
> Sting = 123
> This give: 
> ['1','2','3','12','13','21','23','31','32','123','132','213','231,'312',321']

I think you're missing the list of length 0 ('') in your output...

Later,
Blake.



More information about the Tutor mailing list