-
-
Save jacobgardner/ea202587e46672a89b43 to your computer and use it in GitHub Desktop.
Peter And Sandy
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
from collections import defaultdict | |
# build pairs | |
pairs = [] | |
for i in range(1, 100): | |
for j in range(i, 100): | |
pairs.append((i, j)) | |
def singles_operation(f): | |
results = defaultdict(list) | |
for a, b in pairs: | |
results[f(a, b)].append((a, b)) | |
singles = [] | |
for value in results.itervalues(): | |
# We want to return only the product/sum with one result because if | |
# peter/sandy have this product/sum, then they'll know the two numbers | |
if len(value) == 1: | |
singles.append(value[0]) | |
# Sorted for readability | |
return sorted(singles, key=lambda x: x[0]) | |
def remove_products(): | |
singles = singles_operation(lambda x, y: x * y) | |
print 'peter removed', singles | |
for s in singles: | |
pairs.remove(s) | |
def remove_sums(): | |
singles = singles_operation(lambda x, y: x + y) | |
print 'sandy removed', singles | |
for s in singles: | |
pairs.remove(s) | |
for i in range(7): | |
remove_products() | |
remove_sums() | |
# This is the result because it returns the only pair with a product | |
# not created by anything else left in the list. | |
print 'result', singles_operation(lambda x, y: x * y) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment