Ask Your Question
0

Can you help me write an algorithm that gives me the number of k subsets of a n member set? like {1,2,3}--->two subset---->[{1,2},{1,3},{2,3}]

asked 4 years ago

anonymous user

Anonymous

Can you help me write an algorithm that gives me the number of k subsets of a n member set? like {1,2,3}---two subset---->[{1,2},{1,3},{2,3}]

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
1

answered 4 years ago

This is from the Sage reference manual:

sage: X = Set([1,2,3])
sage: list(X.subsets(2))
[{1, 2}, {1, 3}, {2, 3}]

Or using pure Python tools:

sage: X = set([1,2,3])
sage: import itertools
sage: itertools.combinations(k, 2)
<itertools.combinations object at 0x4572cf470>
sage: list(itertools.combinations(k, 2))
[(1, 2), (1, 3), (2, 3)]
Preview: (hide)
link
0

answered 4 years ago

dan_fulea gravatar image

Yet "an other" possibility, we use the sage specific Combinations. For instance:

sage: S = [1, 2, 3, 5]                                                                                                                                        
sage: Combinations(S, 2)                                                                                                                                      
Combinations of [1, 2, 3, 5] of length 2
sage: for A in Combinations(S, 2): 
....:     print(A) 
....:                                                                                                                                                         
[1, 2]
[1, 3]
[1, 5]
[2, 3]
[2, 5]
[3, 5]
sage:
Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 4 years ago

Seen: 242 times

Last updated: Jun 27 '20