Last active
November 19, 2019 13:09
-
-
Save lovasoa/f2b4ed93e755bf4172583d28f20635a3 to your computer and use it in GitHub Desktop.
Which answer in this list is the correct answer to this question? (see: https://math.stackexchange.com/questions/2217248/which-answer-in-this-list-is-the-correct-answer-to-this-question)
This file contains hidden or 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
-- Run online at: https://repl.it/repls/WeeSlightExams | |
-- hackernews thread: https://news.ycombinator.com/item?id=21543377 | |
solve funcs = filter (matches funcs) (possibilities funcs) | |
matches funcs truths = truths == zipWith (($).($truths).flip) funcs [0..] | |
possibilities [] = [[]] | |
possibilities (x:xs) = concatMap (sequence [(True:), (False:)]) (possibilities xs) | |
all_of = ((and.).) | |
one_of = ((or.).) | |
none_of = (((not.or).).) | |
the_above = take | |
the_below = drop.(+1) | |
main = mapM_ putStrLn $ | |
zipWith | |
(\i s -> "Answer number " ++ (show i) ++ " is " ++ (show s)) | |
[1..] $ | |
head $ solve [ | |
all_of the_below, | |
none_of the_below, | |
all_of the_above, | |
one_of the_above, | |
none_of the_above, | |
none_of the_above | |
] |
This file contains hidden or 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
import itertools | |
# This solves the following question: | |
# https://math.stackexchange.com/questions/2217248/which-answer-in-this-list-is-the-correct-answer-to-this-question | |
# Run online at: https://repl.it/repls/CourteousCreepyAdaware | |
# The code tests all possible combinations of truth values for each of the 6 answers | |
# For each combination, it computes a tuple containing the truth values of the 6 propositions | |
# and checks that it equals the current combination | |
print([ | |
q for q in itertools.product((True, False), repeat=6) | |
if q == ( | |
all(q[1:]), # 1. All of the below. | |
not any(q[2:]), # 2. None of the below. | |
all(q[:2]), # 3. All of the above. | |
any(q[:3]), # 4. One of the above. | |
not any(q[:4]), # 5. None of the above. | |
not any(q[:5]), # 6. None of the above. | |
) | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prolog