tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
20
Task/Power-set/Python/power-set-1.py
Normal file
20
Task/Power-set/Python/power-set-1.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
def list_powerset(lst):
|
||||
# the power set of the empty set has one element, the empty set
|
||||
result = [[]]
|
||||
for x in lst:
|
||||
# for every additional element in our set
|
||||
# the power set consists of the subsets that don't
|
||||
# contain this element (just take the previous power set)
|
||||
# plus the subsets that do contain the element (use list
|
||||
# comprehension to add [x] onto everything in the
|
||||
# previous power set)
|
||||
result.extend([subset + [x] for subset in result])
|
||||
return result
|
||||
|
||||
# the above function in one statement
|
||||
def list_powerset2(lst):
|
||||
return reduce(lambda result, x: result + [subset + [x] for subset in result],
|
||||
lst, [[]])
|
||||
|
||||
def powerset(s):
|
||||
return frozenset(map(frozenset, list_powerset(list(s))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue