Skip to content

Instantly share code, notes, and snippets.

@pipermerriam
Last active August 29, 2015 14:21
Show Gist options
  • Save pipermerriam/d1b5a24230f2f9ec4446 to your computer and use it in GitHub Desktop.
Save pipermerriam/d1b5a24230f2f9ec4446 to your computer and use it in GitHub Desktop.
"""
https://m.whitehouse.gov/blog/2015/05/17/hello-world
Alice and Bob are playing a game. They are teammates, so they will win or lose
together. Before the game starts, they can talk to each other and agree on a
strategy.
When the game starts, Alice and Bob go into separate soundproof rooms – they
cannot communicate with each other in any way. They each flip a coin and note
whether it came up Heads or Tails. (No funny business allowed – it has to be
an honest coin flip and they have to tell the truth later about how it came
out.) Now Alice writes down a guess as to the result of Bob’s coin flip; and
Bob likewise writes down a guess as to Alice’s flip.
If either or both of the written-down guesses turns out to be correct, then
Alice and Bob both win as a team. But if both written-down guesses are wrong,
then they both lose.
The puzzle is this: Can you think of a strategy Alice and Bob can use that is
guaranteed to win every time?
"""
"""
| H | T | Bob || H | T | Alice
--------------------------------
| x | | T || x | | H |
--------------------------------
| x | | T || | x | T |
--------------------------------
| | x | H || x | | H |
--------------------------------
| | x | H || | x | T |
--------------------------------
"""
def bob(res):
if res == 'heads':
return 'tails'
return 'heads'
def alice(res):
if res == 'heads':
return 'heads'
return 'tails'
x = (
('heads', 'heads'),
('heads', 'tails'),
('tails', 'heads'),
('tails', 'tails'),
)
def test_it():
for a_res, b_res in x:
a_guess = alice(a_res)
b_guess = bob(b_res)
if a_guess != b_res and b_guess != a_res:
raise ValueError("A: {0}/{1} - B: {2}/{3}".format(
a_guess, b_res, b_guess, a_res,
))
print "Win!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment