Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''This program will peform the following:
- * A set is provided
- * Each set is converted to a list
- * The subsets of each set is generated
- * The subsets are returned as a frozen set.'''
- def allsubsets(inputset):
- # convert the set to a list, so that it's easier to work with.
- worklist = list(inputset)
- #Using recursion, we establish a base case so that the recursion breaks out.
- if len(worklist) <= 1:
- yield frozenset(worklist)
- yield frozenset([])
- #Generate allsubsets for the list other than the first element
- else:
- #For Each loop that will return each item from the recursive call and returns the subset with the first item in the list added as well
- for item in allsubsets(set(worklist[1:])):
- yield frozenset([worklist[0]]+list(item))
- #Returning just the item itself.
- yield frozenset(item)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement