Created
September 25, 2018 15:10
-
-
Save NeilNjae/ed49f4c9f34cd1e44a6001647405a0ff to your computer and use it in GitHub Desktop.
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
# Solution to Lucy's secret number puzzle, from | |
# http://datagenetics.com/blog/june12018/index.html | |
import collections | |
# how are the four questions answered for this number? | |
def signature(n): | |
return ( n % 2 == 0 | |
, n % 3 == 0 | |
, n % 5 == 0 | |
, n % 7 == 0 | |
) | |
# For each "signature", what numbers share that signature? | |
signatures = collections.defaultdict(list) | |
for i in range(1, 100): | |
signatures[signature(i)] += [i] | |
# What signatures have just one number each? | |
[(k, v) for k, v in signatures.items() if len(v) == 1] | |
# -> [((False, False, True, True), [35]), ((True, False, True, True), [70])] | |
# The solution is obvious by inspection: if you heard the first question and answer, | |
# you know the number. If the answer was "Yes" (i.e. True), the number is 70. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment