-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn-choose-3.py
35 lines (28 loc) · 1.16 KB
/
n-choose-3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#Implement n choose 3 to find all possible subsets of size 3
def printCombinations(cards):
count=0
combinations=[]
for i in range(len(cards)):
for j in range((i+1),len(cards)):
for k in range((j+1), len(cards)):
print(cards[i], cards[j], cards[k])
combinations.append([cards[i], cards[j], cards[k]])
count+=1
print('Combination Count =',count)
return combinations
def main():
cards=['a','b','c','d','e','f']
combinations=printCombinations(cards)
print("\nAre Combinations unique?", areCombinationsDistinct(combinations))
combinations.append(['a', 'b', 'c'])
print("\nAdded duplicate combination ['a', 'b', 'c'] to test uniqueness")
print("Are Combinations unique?", areCombinationsDistinct(combinations))
#Search for duplicates to verify n choose 3 algorithm works
def areCombinationsDistinct(combinations):
for i in range(len(combinations)):
for j in range((i+1), len(combinations)):
#Check if another combination contains all 3 cards that the current combination contains
if (combinations[i][0] in combinations[j]) and (combinations[i][1] in combinations[j]) and (combinations[i][2] in combinations[j]):
return False
return True
main()