Created
January 12, 2019 14:00
-
-
Save bluenex/7b7a28b24ecdf81bf54f5e9ac7306db8 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import itertools" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"questions = [\n", | |
" (1, '4433', 6),\n", | |
" (2, '8699', 19),\n", | |
" (3, '8978', 20),\n", | |
" (4, '8173', 58),\n", | |
" (5, '3459', 50),\n", | |
" (6, '3528', 17),\n", | |
" (7, '7378', 135),\n", | |
" (8, '3766', 587),\n", | |
" (9, '4245', 471),\n", | |
" (10, '2783', 844),\n", | |
" (11, '7856', 535),\n", | |
" (12, '1395', 80),\n", | |
" (13, '4549', 96)\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" >> (((4-4)+3)+3) = 6, ('-', '+', '+')\n", | |
"1: 15 ways to solve.\n", | |
" >> (((9-6)*9)-8) = 19, ('-', '*', '-')\n", | |
"2: 1 ways to solve.\n", | |
"3: 0 ways to solve.\n", | |
" >> (((8*7)-1)+3) = 58, ('*', '-', '+')\n", | |
"4: 4 ways to solve.\n", | |
" >> (((4**3)-5)-9) = 50, ('**', '-', '-')\n", | |
"5: 6 ways to solve.\n", | |
" >> (((3+2)*5)-8) = 17, ('+', '*', '-')\n", | |
"6: 17 ways to solve.\n", | |
"7: 0 ways to solve.\n", | |
"8: 0 ways to solve.\n", | |
"9: 0 ways to solve.\n", | |
"10: 0 ways to solve.\n", | |
"11: 0 ways to solve.\n", | |
"12: 0 ways to solve.\n", | |
"13: 0 ways to solve.\n" | |
] | |
} | |
], | |
"source": [ | |
"# operators_perm = list(itertools.permutations(['+', '-', '*', '/'], 3))\n", | |
"# duplicable\n", | |
"operators_perm = list(itertools.product(['+', '-', '*', '/', '**'], repeat=3))\n", | |
"\n", | |
"# loop through all questions\n", | |
"for n, q, ans in questions:\n", | |
" # a flag for 'first answer is found'\n", | |
" got_for_n = False\n", | |
" # a list to store solved question string to get unique questions\n", | |
" solver = []\n", | |
" # loop through permutations of question\n", | |
" for qp in itertools.permutations(list(q)):\n", | |
" # temp string with {} to format in operators later\n", | |
" s = '{}'.join(qp)\n", | |
" # add parentheses for correct order of calculation\n", | |
" with_oprt_space = '((({}){}){})'.format(s[:4], s[4:7], s[7:])\n", | |
" # loop through permutations of operators defined above\n", | |
" for oprt in operators_perm:\n", | |
" # construct a question to test (add operators into question)\n", | |
" test_question = with_oprt_space.format(oprt[0], oprt[1], oprt[2])\n", | |
" # if result of test question matches with answer\n", | |
" if eval(test_question) == ans:\n", | |
" if not got_for_n:\n", | |
" # print if this is the first answer\n", | |
" print(' >> {} = {}, {}'.format(test_question, ans, oprt))\n", | |
" # set this flag to True to avoid printing again\n", | |
" # (only print once for each question)\n", | |
" got_for_n = True\n", | |
" # also collect question in a list to get uniques later\n", | |
" solver.append(test_question)\n", | |
" # get unique way of current loop question\n", | |
" ways = list(set(solver))\n", | |
" # print how many ways this question can be solved\n", | |
" print(\"{}: {} ways to solve.\".format(n, len(ways)))" | |
] | |
} | |
], | |
"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.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment