Ask Your Question
0

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

asked 2020-05-29 18:49:45 -0500

updated 2020-05-30 04:59:01 -0500

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-29 19:34:25 -0500

tmonteil gravatar image

updated 2020-05-29 19:42:56 -0500

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-29 18:49:45 -0500

Seen: 33 times

Last updated: May 29