Here is my Python Code:
- Code: Select all
def powerset3(a):
if (len(a) == 0):
return frozenset({})
else:
s=a.pop()
b=frozenset({})
b|=frozenset({})
b|=frozenset({s})
for subset in powerset3(a):
b|=frozenset({str(subset)})
b|=frozenset({s+subset})
return b
If I run the program with:
- Code: Select all
print(powerset3(set(['a', 'b'])))
I get following solution
- Code: Select all
frozenset({'a', 'b', 'ab'})
But I want to have
- Code: Select all
{frozenset(), frozenset({'a'}), frozenset({'b'}), frozenset({'b', 'a'})}
I would rather not use libraries and it ought to be recursive! Also before trying this all stuff I have gone through several resources on the web and read a couple of articles onfrozenset in Python to understand the concept in a better way, my source of Information: https://www.quora.com/What-is-Frozenset-in-Python, https://www.scaler.com/topics/frozenset-in-python/ and GFG.
Gratitude for your assistance