Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I am not quite sure what should be implemented, i think it is the following recursive function:

def my_subsets(S):
    if len(S) == 0:
        return Set( [ Set([]) ] )
    s = S[0]
    T = S.difference( Set( [s] ) )
    PT = my_subsets(T)
    return PT + Set( [ Set([s]) + A for A in PT ] )

For instance:

for A in my_subsets( Set( [1,2,3,'abc'] ) ):
    print A

which delivers:

{'abc'}
{1, 2}
{}
{2, 'abc'}
{1, 3}
{1, 2, 'abc'}
{1, 3, 'abc'}
{3}
{2}
{2, 3}
{1}
{1, 'abc'}
{2, 3, 'abc'}
{1, 2, 3, 'abc'}
{1, 2, 3}
{3, 'abc'}

where

sage: len( my_subsets( Set( [1,2,3,'abc'] ) ) )
16

(The shown order the elements is completely random, as one may expect. If the order is an issue, then one should use "unique" lists...)