[Tutor] Recursive permutation

x x unigeek at hotmail.com
Fri Nov 19 18:59:38 CET 2004


Here is my 2 bits worth.
If I understand what you want you need only use the Result and make a new 
list that takes each item and creates all possible orders of that item.

Result = ['']

def Combination(S):

    if len(S)>0:
        if S not in Result:
            Result.append(S)
            for I in range(len(S)):
                str2= S[0:I] + S[I+1:len(S)]
                Combination(str2)

    return Result


Combination("123456")
Result.sort()

print Result




>From: Blake Winton <bwinton at latte.ca>
>To: Eivind Triel <ET at cheminova.dk>, tutor at python.org
>Subject: Re: [Tutor] Recursive permutation
>Date: Thu, 18 Nov 2004 10:16:12 -0500
>
>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.
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor

_________________________________________________________________
Don't just Search. Find! http://search.sympatico.msn.ca/default.aspx The new 
MSN Search! Check it out!



More information about the Tutor mailing list