Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
re.search & eval solution in Clear category for Safe Code by Sim0000
import re
def safe_code(equation):
eq = equation.replace('=', '==')
f = bool(re.search('(^|[^0-9])##|#[0-9]', eq))
for n in "0123456789"[f:]:
if n not in eq and eval(eq.replace('#', n)): return int(n)
return -1
print(safe_code('1-1=##'))
print(safe_code('111-11=1##'))
print(safe_code('#+#=#*#'))
print(safe_code('##=##'))
print(safe_code('##=5-5'))
if __name__ == '__main__':
print("Example:")
print(safe_code("-5#*-1=5#"))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert safe_code("-5#*-1=5#") == 0
assert safe_code("##*##=302#") == 5
assert safe_code("19--45=5#") == -1
assert safe_code("##--11=11") == -1
assert safe_code("#9+3=22") == 1
assert safe_code("11*#=##") == 2
assert safe_code("#9+3=12") == -1
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 22, 2018
Comments: