Created
October 11, 2018 18:54
-
-
Save akbo/25344460ed35397e509327cb50f24bd4 to your computer and use it in GitHub Desktop.
Generate a card deck like the one used in the game Dobble Kids
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
""" | |
There are 31 different animals and we want to have a deck of cards such that on each card there are 6 animals and every card has exactly one animal in common with every other card. Our card deck should have at least 30 cards. | |
""" | |
from itertools import combinations | |
from random import shuffle | |
from pprint import pprint | |
n_animals = 31 | |
n_animals_per_card = 6 | |
print( | |
"number of possible combinations:", | |
len(list(combinations(range(n_animals), n_animals_per_card))), | |
) | |
decks = [] | |
cards = [] | |
while len(cards) < 30: | |
combs = list(combinations(range(n_animals), n_animals_per_card)) | |
shuffle(combs) | |
cards = [set(combs[0])] | |
for combination in combs: | |
valid_comb = True | |
combination_set = set(combination) | |
for card in cards: | |
if not len(card.intersection(combination_set)) == 1: | |
valid_comb = False | |
break | |
if valid_comb: | |
cards.append(combination_set) | |
cards = [list(card) for card in cards] | |
print("size of current carddeck:", len(cards)) | |
decks.append(cards) | |
pprint(decks[-1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment