Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
No eval, just operators solution in Clear category for Safe Code by BrianMcleod
import operator
import re
def safe_code(equation):
ops = {'+':operator.add, '*':operator.mul, '-':operator.sub}
zero_test = not (equation[0] == '#' or re.search(r'[-=*+]#', equation))
for i in range(10):
a, b, c = map(int, re.findall(r'-?\d+', re.sub(r'(\d-)(\d)', r'\1 \2', equation.replace('#', str(i)))))
if ops[re.search(r'[#0-9](?P[+*-])', equation).group('op')](a, b) == c:
if (i != 0 or zero_test) and str(i) not in equation:
return i
return -1
Sept. 18, 2018
Comments: