Skip to content

Instantly share code, notes, and snippets.

@jacobgardner
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobgardner/ea202587e46672a89b43 to your computer and use it in GitHub Desktop.
Save jacobgardner/ea202587e46672a89b43 to your computer and use it in GitHub Desktop.
Peter And Sandy
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