Permutations algoritm?

William Park opengeometry at yahoo.ca
Fri Nov 15 18:22:58 EST 2002


sismex01 at hebmex.com wrote:
>> From: William Park [mailto:opengeometry at yahoo.ca]
>> Sent: Friday, November 15, 2002 3:42 PM
>> 
>> sismex01 at hebmex.com wrote:
>> > Does anybody have a clear, simple, permutations algorithm
>> > in Python?
>> 
>> Would you care to explain this?  Permutation can mean 
>> different things.
>> Concrete example might be good...
>>
> 
> Of a set of different items 'S', obtain all distinct subsets of 'n'
> items where all items in the subset are different.
> 
> So, if I have, for example:
> 
>   S = [ 0, 1, 2, 3 ]
> 
> the universe of available subsets of 3 items would be:
> 
>   s = [ (0, 1, 2),
>         (0, 1, 3),
>         (0, 2, 3),
>         (1, 2, 3) ]
> 
> the universe of available subsets of 2 items would be:
> 
>   s = [ (0, 1),
>         (0, 2),
>         (0, 3),
>         (1, 2),
>         (1, 3),
>         (2, 3) ]
> 
> Any help?

So, you don't want just a number (ie. P(n,k) or C(n,k)), but instead you
want list of sets spat out...  Well, using your example, try

    s, N = [], len(S)
    for a in range(N):
	for b in range(a+1, N):
	    for c in range (b+1, N):
		s.append(a, b, c)
    
-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
Linux solution for data management and processing. 



More information about the Python-list mailing list