Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Calculator-VI by kisungch
OP = ('+', '-', '*', '/', '//', '%', '**')
OPS = ('+', '-', '*', '/', '//', '%', '**', '=', '+=', '-=', '*=', '/=', '//=', '**=')
def calculator(log: str) -> str:
display, op, prev, state, i = '', '', '', 0, 0
while i < len(log):
if log[i] == 'C':
display, op, prev, state = '', '', '', 0
i += 1
continue
if log[i] == 'B':
if op != '' and op[-1] in OP:
op = op[:-1]
elif display !='':
display = display[:-1]
state = 1
i += 1
continue
if log[i] in '+-' and i+1 < len(log) and log[i+1] in '+-':
i += 1
continue
if log[i] == '/' and i+1 < len(log) and log[i+1] == '/':
c = '//'
i += 1
elif log[i] == '*' and i+1 < len(log) and log[i+1] == '*':
c = '**'
i += 1
else:
c = log[i]
if c in OP and i+1 < len(log) and log[i+1] == '=':
c += log[i+1]
i += 1
if c in OPS:
if len(c) > 1 and c[-1] == '=':
op = display if op != '' and op[-1] not in OP and 'error' not in op else op + display
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] + display if op != '' else ''
else:
prev = ''
op = display if op != '' and op[-1] not in OP and 'error' not in op else op + display
else:
if not(i+1 < len(log) and log[i+1] == '='):
prev = ''
op = display if op != '' and op[-1] not in OP and 'error' not in op else op + display
if 'error' in op:
return 'error'
display = str(eval(op)) if op != '' else ''
if display != 'error' and display != '' and '.' in display:
nums = display.split('.')
if int(nums[0]) > 9999:
display = 'error'
elif len(nums) == 2:
num = '.'.join(['0', nums[1]])
display = str(int(nums[0]) + round(float(num), len(nums[1])-(len(display)-5)))
display= display[:-1] if display[-1] == '0' else display
display = 'error' if float(display) > 9999. else display
elif not (display == 'error' or display == '') and abs(eval(display)) > 99999:
display = 'error'
op = display if c == '=' else display + c
op = 'error' if display == 'error' else op
state = 0
i += 1
else:
while i < len(log) and log[i] in '0123456789.':
if state == 1:
display += log[i]
elif log[i] != '0':
display = log[i]
state = 1
i += 1
display = display[:5] if len(display) > 5 else display
return display if display != '' else '0'
print("Example:")
print(calculator("123BB"))
# These "asserts" are used for self-checking
assert calculator("123BB") == "1"
assert calculator("123C12*=B") == "14"
assert calculator("123BBBBBB") == "0"
assert calculator("C") == "0"
assert calculator("-++B") == "0"
assert calculator("10/2*2=") == "10."
assert calculator("10/=*=-=") == "0."
assert calculator("100//33**3=") == "27"
assert calculator("10%10=") == "0"
assert calculator("---+++100//3//3+++---") == "11"
assert calculator("27**.3333=") == "3."
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
Comments: