Created
May 14, 2023 05:07
-
-
Save hannal/360542e731e03ddd4f4bb1cc37e2a137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import combinations | |
from math import factorial | |
def count_combinations(n: int, r: int): | |
return int(factorial(n) / (factorial(r) * factorial(n-r))) | |
team_count = 4 | |
# 0 1 2 3 4 5 6 7 8 9 10 11 | |
people = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"] | |
result = tuple(combinations(people, team_count)) | |
total = count_combinations(len(people), team_count) # same as `len(result)` | |
# 1 | |
comb1 = result[0] # a, b, c, d | |
# 2 | |
index1 = count_combinations(len(people)-1, team_count-1) | |
index2 = count_combinations(len(people)-4, team_count-1) # 4 == 'e' index | |
comb2 = result[index1-index2] # a, e, f, g | |
# 3 | |
index1 = count_combinations(len(people)-1, team_count-1) | |
index2 = count_combinations(len(people)-7, team_count-1) # 7 == 'h' index | |
comb3 = result[index1-index2] # a, h, i, j |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test comment