Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Expected less instructions ! solution in Clear category for Calculator-I by dig
import re
def calculator(log: str) -> str:
if set(log)=={'0'} or log in('', '+', '-', '='): return '0'
log=re.sub('^0*','',log)
log=re.sub('\+0+','+',log)
log=re.sub('-0+','-',log)
log=re.sub('^[\+=]*','',log)
if log[-1] not in ('+', '-', '=') and log.isdigit()==False:
return re.search('\d+$', log)[0]
if log[-1] in ('=','-','+'):
return str(eval(log[:-1]))
return log
print("Example:")
print(calculator("1+2"))
# These "asserts" are used for self-checking
assert calculator("000000") == "0"
assert calculator("0000123") == "123"
assert calculator("12") == "12"
assert calculator("+12") == "12"
assert calculator("") == "0"
assert calculator("1+2") == "2"
assert calculator("2+") == "2"
assert calculator("1+2=") == "3"
assert calculator("1+2-") == "3"
assert calculator("1+2=2") == "2"
assert calculator("=5=10=15") == "15"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Feb. 16, 2023