Skip to content

Instantly share code, notes, and snippets.

@patrickpang
Last active October 23, 2018 03:32
Show Gist options
  • Save patrickpang/d02be531628eb2ad99ae56fa6c95d25a to your computer and use it in GitHub Desktop.
Save patrickpang/d02be531628eb2ad99ae56fa6c95d25a to your computer and use it in GitHub Desktop.
3 cards with numbers from 1 to 9 are distributed to 3 people in a few rounds. The final sums of numbers in cards received by them are 13, 15, 23 respectively. Find the numbers on the cards.
#!/usr/bin/env python3
from random import shuffle
def trial(x, y, z):
numbers = [x, y, z]
sums = [0, 0, 0]
while all([n < 23 for n in sums]):
shuffle(numbers)
sums = [sums[0] + numbers[0], sums[1] +
numbers[1], sums[2] + numbers[2]]
print(sums)
if sorted(sums) == [13, 15, 23]:
return True
return False
candidates = list(range(1, 10))
while True:
shuffle(candidates)
x, y, z = candidates[0], candidates[1], candidates[2]
if trial(x, y, z):
print('Success', x, y, z)
break
else:
print('Failure', x, y, z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment