Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Taking a RE-test solution in Clear category for Safe Code by HeNeArKr
import re
NUMBERS = set('0123456789')
def safe_code(equation):
""" Return smallest digit that satisfies the 'equation' when replacing all
of the '#'
"""
used = {ch for ch in equation if ch.isdecimal()}
for possible in sorted(NUMBERS - used): # sort -> return smallest digit
# Do not consider cases where '0' is first char in line or follwing
# an operator
if possible == '0' and any(re.findall(r'^#+|[+\-*=]#+', equation)):
continue
result = equation.replace('#', possible)
if eval(result.replace('=', '==')):
return int(possible)
return -1
Sept. 17, 2018