Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Safe Code by brownie57
import re
def safe_code(equation):
for i in (j for j in range(10) if str(j) not in equation):
if i == 0 and re.search(r'\D?#[\d|#]', equation):
continue
eq = equation.replace('#', str(i)).split('=')
if eval(eq[0]) == int(eq[1]):
return i
return -1
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
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 18, 2018
Comments: