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 2020-06-19 22:24:45 +0200

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}]

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2020-06-27 03:11:55 +0200

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:
edit flag offensive delete link more
1

answered 2020-06-19 23:47:19 +0200

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)]
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

1 follower

Stats

Asked: 2020-06-19 22:24:45 +0200

Seen: 184 times

Last updated: Jun 27 '20