Monday, 1 February 2016

Powerset Generator Recursive Algorithm Python

 Q:Write a python program to generate powerset for given set of elements.


Code

def generator(n,l):
    l=list(l)
    if n==len(powstr)-1:
        print l
        l.append(powstr[n])
        print l
    else:
        generator(n+1, l)
        l.append(powstr[n])
        generator(n+1,l)
        
        
        
        
print "powerset generator"
print "program by Abhishek Munagekar"
print "for programing wonders"
print "enter the string for which the powerset is to be generated"
powstr=raw_input()
l=list()
generator(0,l)

Output

powerset generator
program by Abhishek Munagekar
for programing wonders
enter the string for which the powerset is to be generated
123
[]
['3']
['2']
['2', '3']
['1']
['1', '3']
['1', '2']
['1', '2', '3']

No comments:

Post a Comment