frozenset in Python

Posted on
Wed Jul 27, 2022 5:59 am
Lakshit offline

frozenset in Python

I'm staying here for just about 5 hours attempting to tackle the issue and presently I'm expecting your assistance.

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

Posted on
Wed Jul 27, 2022 9:01 am
jay (support) offline
Site Admin
User avatar
Posts: 18221
Joined: Mar 19, 2008
Location: Austin, Texas

Re: frozenset in Python

I don't have any experience with frozenset - it's a rather esoteric data structure.

Again, like your other post, I'm unclear on exactly what it is you are trying to accomplish. I suspect that you're overthinking it. Please explain what you want to do at a high level (not using python specific things like frozenset).

[MODERATOR NOTE]: moved to a more appropriate forum.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 8 guests