Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
modified solution in Clear category for Calculator-IV by kisungch
OP = ('+', '-')
OPS = ('+', '-', '=')
def calculator(log: str) -> str:
current, op, prev, i = '', '', '', 0
while i < len(log):
if log[i] in OPS:
if log[i] in OP and i+1 < len(log) and log[i+1] in OP:
i += 1
continue
if log[i] in OP and i+1 < len(log) and log[i+1] == '=':
c = log[i] + log[i+1]
i += 1
else:
c = log[i]
if c == '-=' or c == '+=':
op = current if op != '' and op[-1] not in OP and 'error' not in op else op + current
if op != '':
op = str(eval(op))
prev = c[0] + op
op += prev
c = '='
else:
i += 1
continue
elif c == '=' and prev != '':
op += prev
elif c == '=':
if i+1 < len(log) and log[i+1] == '=':
prev = op[-1] + current if op != '' else ''
else:
prev = ''
op = current if op != '' and op[-1] not in OP and 'error' not in op else op + current
else:
if not(i+1 < len(log) and log[i+1] == '='):
prev = ''
op = current if op != '' and op[-1] not in OP and 'error' not in op else op + current
if 'error' in op:
return 'error'
current = str(eval(op)) if op != '' else ''
if current != 'error' and current != '' and '.' in current:
nums = current.split('.')
if int(nums[0]) > 9999:
current = 'error'
else:
num = '.'.join(['0', nums[1]])
current = str(int(nums[0]) + round(float(num), len(nums[1])-(len(current)-5)))
current = current[:-1] if current[-1] == '0' else current
current = 'error' if float(current) > 9999. else current
elif not (current == 'error' or current == '') and abs(eval(current)) > 99999:
current = 'error'
op = current if c == '=' else current + c
op = 'error' if current == 'error' else op
i += 1
else:
current = ''
while i < len(log) and log[i] in '0123456789.':
current = current + log[i] if current != '' or log[i] != '0' else current
i += 1
current = current[:5] if len(current) > 5 else current
return current if current != '' else '0'
print("Example:")
print(calculator("0001.2005+=-1+10.43+99979="))
# These "asserts" are used for self-checking
assert calculator("0001.1000") == "1.100"
assert calculator("0001.1000-") == "1.1"
assert calculator("999.9999999+=") == "2000."
assert calculator("1.000123") == "1.000"
assert calculator("9999.9999999+=") == "error"
assert calculator("90000+10000=") == "error"
assert calculator("90000+10000-10000=") == "error"
assert calculator("90000+10000-10000") == "10000"
assert calculator("123456789") == "12345"
assert calculator("123456789+5=") == "12350"
assert calculator("5+123456789") == "12345"
assert calculator("50000+=") == "error"
assert calculator("3+=") == "6"
assert calculator("3+2==") == "7"
assert calculator("4-1==") == "2"
assert calculator("3+-2=") == "1"
assert calculator("-=-+3-++--+-2=-") == "1"
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!")
Aug. 12, 2023