Ask Your Question
0

Please help me write a function that receives a set and then gives all its subsets

asked 2020-05-30 01:49:45 +0100

updated 2020-05-30 11:59:01 +0100

FrédéricC gravatar image

Please help me write a function that receives a set and then gives all its subsets

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2020-05-30 02:34:25 +0100

tmonteil gravatar image

updated 2020-05-30 02:42:56 +0100

For Sage Set (see the uppercase), there is a method that does that out of the box:

sage: s = Set([1,2,3]) ; s
{1, 2, 3}
sage: s.subsets()
Subsets of {1, 2, 3}
sage: list(s.subsets())
[{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]

For Pyyhon set (see the lowercase) you can use some itertools or go back and forth to Sage Sets :

sage: def powerset(s):
....:     return set(Set(s).subsets())
sage: S = {1,2,3}
sage: powerset(S)
{{1, 3}, {}, {1}, {3}, {1, 2, 3}, {1, 2}, {2, 3}, {2}}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2020-05-30 01:49:45 +0100

Seen: 272 times

Last updated: May 30 '20