Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Safe Code by lyq3413
import re
def safe_code(equation):
for i in range(10):
if re.search(('^#|##|#[0-9]]'), equation) and i == 0 or str(i) in equation:
continue
equation_str = equation.replace('#', str(i)).replace('=', '==')
if eval(equation_str):
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
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!")
March 12, 2019