2. Card guessing game¶
2.1. Rules of the game¶
Consider a deck of 52 cards:
1 (A) | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 (J) | 12 (Q) | 13 (K) | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diamond | |||||||||||||
Club | |||||||||||||
Heart | |||||||||||||
Spade |
Each card is in one of the four suits: Diamond, Club, Heart, and Spade.
Each card has a value \(1 \text{ (A)} < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 \text{ (J)} < 12 \text{ (Q)} < 13 \text{ (K)}\).
The following code creates a deck of cards. (You do not need to understand the code for now.)
# Create a deck of cards
from collections import namedtuple
suits = ("Diamond", "Club", "Heart", "Spade")
values = range(1, 14)
Card = namedtuple('Card', ['value', 'suit'])
deck = [Card(value, suit) for value in values for suit in suits]
print(deck)
To play the game, a dealer randomly pick a card without letting you know, and you’re going to guess what exactly that card is.
# Randomly draw a card from the deck with replacement
import random
print(random.choice(deck))
You are allowed to make an informed guess after the dealer answers some of your yes/no questions.
For instance, you may ask:
Is the suit club?
Is the card diamond 1 (ace)?
Is the value at least 10?
However, you cannot ask:
What is the value?
What is the suite?
Exercise You win if you can guess the card correctly with no more than 6 questions. What is the winning strategy?
YOUR ANSWER HERE
Hint 1: Obviously, you should not ask whether the card is precisely certain card, e.g., Is it Diamond Ace? Is it Diamond 2? … Why not? The card may be one of the remaining \(52-6=46\) possibilities you did not ask.
Hint 2: Think of each Yes/No question as splitting the set of possible cards into two smaller groups of possible cards corresponding to each possible answer Yes/No.
Hint 3: How many questions is required to split the set of 52 cards into groups of size \(1\), i.e., with only one possible card?
2.2. Challenge the computer¶
Play the role of the dealer and test if the program below can guess the card correctly after 6 questions.
suitIdx = 0
number = 0
if "y" == input(
"Is the suite either heart or spade? (y/[n]) ").strip().lower():
suitIdx += 2
if "y" == input("Is the suite either club or spade? (y/[n]) ").strip().lower():
suitIdx += 1
if "y" == input(
f"Is the number {number+8} or above? (y/[n]) ").strip().lower():
number += 8
if "y" == input(
f"Is the number {number+4} or above? (y/[n]) ").strip().lower():
number += 4
if "y" == input(
f"Is the number {number+2} or above? (y/[n]) ").strip().lower():
number += 2
if "y" == input(
f"Is the number {number+1} or above? (y/[n]) ").strip().lower():
number += 1
print(f"The card is {suits[suitIdx]} {number}")
Exercise Does the above program always win? Explain your answer?
YOUR ANSWER HERE
2.3. Challenge your understanding¶
The following table gives the binary representions of unsigned decimal integers from 0 to 7.
Binary | Decimal |
---|---|
000 | 0 |
001 | 1 |
010 | 2 |
011 | 3 |
100 | 4 |
101 | 5 |
110 | 6 |
111 | 7 |
To convert binary to decimal, think of the conversion as a guessing game where
the binary sequence is a sequence of yes (1) or no (0) answers to certain yes/no questions, and
the informed guess is the integer represented by the binary sequence.
For instance, observe that the binary representation of 4, 5, 6, and 7 actually have 1 in the leftmost (most significant) bit. Therefore we can consider that bit as the answer to the following yes/no question:
Is the integer 4 or above?
Exercise What are the yes/no questions corresponding to the 2nd bit and 3rd bit?
YOUR ANSWER HERE