Created
March 20, 2018 21:11
-
-
Save ewjoachim/7db0ded933ea570e1a3af1d68fa8742f to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from collections import defaultdict\n", | |
"s = defaultdict(list) # sums\n", | |
"p = defaultdict(list) # products" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(96, 1179)" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Indexing pairs by their sums and products\n", | |
"for x in range(2, 100):\n", | |
" for y in range(x + 1, 100 - x + 1):\n", | |
" s[x + y].append((x, y))\n", | |
" p[x * y].append((x, y))\n", | |
"len(s), len(p)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"574" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Keeping only the sums and products that have several pairs that could have generated them\n", | |
"# Alice says she doesn't know (and Bob doesn't know yet)\n", | |
"p2 = {k: v for k, v in p.items() if len(v) > 1}\n", | |
"len(p2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"10" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Bob says he knew alice didn't know, so it means all the pairs he has have products\n", | |
"# that could be generated by other pairs\n", | |
"s2 = {k: v for k, v in s.items() if all(len(p.get(a*b, []))>1 for a, b in v)}\n", | |
"len(s2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"86" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Alice says she now knows, which means she has a product that only has one value from s2\n", | |
"s2set = set(a for b in s2.values() for a in b)\n", | |
"p3set = {e.pop() for e in (set(v) & s2set for v in p2.values()) if len(e) == 1}\n", | |
"len(p3set)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Bob says he knows too, which means he has a sum that only has one value from p3\n", | |
"s3set = {e.pop() for e in (set(v) & p3set for v in s2.values()) if len(e) == 1}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{(4, 13)}" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"s3set" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment