Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Calculator-I by wo.tomasz
from functools import reduce
def preparation(log):
if len(log) > 0 and log[0] == "-":
log = log[1:]
return "0" + log
def calculate(log):
log = log.replace("+", " ").replace("-", " -")
return str(int(reduce(lambda x, y: int(x) + int(y), log.split(" "))))
def last_val(log):
log = log.replace("+", " ").replace("-", " ").replace("=", " ")
return str(int(log.split(" ")[-1]))
def calculator(log: str) -> str:
res = ""
log = preparation(log)
if log[-1].isdigit():
res = last_val(log)
else:
res = calculate(log[:-1])
return res
Feb. 2, 2023
Comments: