Each card in the multi-player card game Dobble displays eight different symbols. Any two cards have exactly one symbol in common.
The game is played by dealing one card to each player and placing the remaining cards face-up in the center of the table. The first player to spot the symbol that matches the top card on his pile to the top card on the central pile calls out the name of the symbol and moves the top card from the central pile to the top of his own pile. The game continues until all cards in the central pile have been claimed. The winner is the player with the most cards.
There are multiple versions of Dobble that vary in the symbols used, the number of cards and the number of symbols per card. However, the cards always meet the following conditions:
the number of cards equals the number of different symbols
each card has $$n$$ symbols displayed on it (for a given $$n$$)
each symbol is displayed on $$n$$ cards (the same $$n$$ as above)
any two cards have exactly one symbol in common
any two symbols are displayed together on exactly one card
A deck of cards contains cards that display a number of different symbols. These cards do not necessarily have to meet the conditions of the card game Dobble. A symbol is represented as a string (str) that contains no commas. The deck of cards is described in a text file whose lines contain a comma-separated list of symbols on a single card. For example, such a text file might look like this:
cheese,ice cube,snowman,water drip,light bulb,heart,eye eye,snowman,treble clef,ice cube,exclamation point,scissors,zebra,red lips ice cube,zebra,light bulb,red lips,treble clef,snowman,daisy flower light bulb,treble clef,snowman,eye,daisy flower,heart,zebra exclamation point,zebra,light bulb,cheese,daisy flower,ice cube treble clef,zebra,daisy flower,snowman,water drip,exclamation point,light bulb red lips,daisy flower,exclamation point,cheese,eye,zebra heart,zebra,scissors,daisy flower,snowman,exclamation point red lips,exclamation point,water drip,scissors,ice cube water drip,light bulb,eye,red lips,heart cheese,snowman,daisy flower,exclamation point,ice cube,water drip red lips,scissors,heart,ice cube,daisy flower,exclamation point
Based on their description in the text file, the cards in the deck are numbered consecutively, starting from 1. The first line of the text file therefore corresponds to card 1, the second line to card 2, and so on. Your task:
Write a function card2symbols that takes the location (str) of a text file describing a deck of cards. The function must return a dictionary (dict) that maps the number (int) of each card in the deck onto the set (set) of symbols (str) on the card.
Write a function common_symbols that takes three arguments. The first two arguments are the numbers (int) of two cards in a deck of cards. The third argument is a dictionary (dict; as returned by the function card2symbols) that describes the deck of cards. The function must return a set (set) containing all symbols (str) that are displayed on the two given cards.
Write a function symbol2cards that takes a dictionary (dict; as returned by the function card2symbols) that describes a deck of cards. The function must return a new dictionary (dict) that maps each symbol (str) on the cards onto a set (set) containing all numbers (int) of the cards that display the symbol.
Write a function common_cards that takes three arguments. The first two arguments are symbols (str) that are displayed on the deck of cards. The third argument is a dictionary (dict; as returned by the function symbol2cards) indicating on which cards each symbol is displayed. The function must return a set (set) containing all cards (int) on which the two given symbols are displayed together.
Write a function missing_card that takes the location (str) of a text file describing a deck of cards as used in the game of Dobble (meeting the conditions as outlined in the introduction). Unfortunately, one of the cards in the deck got lost. The function must return a set (set) containing all symbols (str) on the missing card.
In the following interactive session we assume the text files cards.txt1 and missing.txt2 to be located in the current directory.
>>> c2s = card2symbols('cards.txt')
>>> c2s[1]
{'cheese', 'eye', 'heart', 'ice cube', 'light bulb', 'snowman', 'water drip'}
>>> c2s[2]
{'exclamation point', 'eye', 'ice cube', 'red lips', 'scissors', 'snowman', 'treble clef', 'zebra'}
>>> c2s[3]
{'daisy flower', 'ice cube', 'light bulb', 'red lips', 'snowman', 'treble clef', 'zebra'}
>>> common_symbols(1, 2, c2s)
{'eye', 'ice cube', 'snowman'}
>>> common_symbols(1, 3, c2s)
{'ice cube', 'light bulb', 'snowman'}
>>> common_symbols(2, 3, c2s)
{'ice cube', 'red lips', 'snowman', 'treble clef', 'zebra'}
>>> s2c = symbol2cards(c2s)
>>> s2c['snowman']
{1, 2, 3, 4, 6, 8, 11}
>>> s2c['ice cube']
{1, 2, 3, 5, 9, 11, 12}
>>> s2c['zebra']
{2, 3, 4, 5, 6, 7, 8}
>>> common_cards('snowman', 'ice cube', s2c)
{1, 2, 3, 11}
>>> common_cards('ice cube', 'zebra', s2c)
{2, 3, 5}
>>> common_cards('zebra', 'snowman', s2c)
{2, 3, 4, 6, 8}
>>> missing_card('missing.txt')
{'baby bottle', 'carrot', 'clown', 'daisy flower'}