实现一个计算器,该计算器将可以在浏览器路由路径中写入的值作为单个字符串。然后它将结果作为数字(或错误消息)返回。
路由路径使用“/”符号,所以这不能在我们的计算器中。我们使用“$”符号作为除法运算符。
您将收到一个包含数字和运算符的任意长度的字符串:
- ‘+’ = 添加
- ‘-’ = 减法
- ‘*’ = 相乘
- ‘$’ = 除法
您的任务是分解字符串并使用此操作顺序计算表达式。(除法 => 乘法 => 减法 => 加法)
如果给您一个无效的输入(即除 之外.0123456789+-*$
的任何字符),您应该返回错误消息:'400: Bad request'
记住:
- 操作次数不受限制
- 操作顺序是必须的
- 无
eval
或等效功能
- 将数字转换为
floats
,而不是整数
例子:
calculate('1+1') => '2'
calculate('10$2') => '5'
calculate('1.5*3') => '4.5'
calculate('5+5+5+5') => '20'
calculate('1000$2.5$5+5-5+6$6') =>'81'
calculate('10-9p') => '400: Bad request'
题目难度:一般
题目来源:https://www.codewars.com/kata/581bc0629ad9ff9873000316
def calculate(expression):
# your code here
assert calculate("1.1") == 1.1
assert calculate("10+5") == 15
assert calculate("8-2") == 6
assert calculate("4*3") == 12
assert calculate("18$2") == 9
assert calculate("5+8-8*2$4") == 9
assert calculate("3x+1") == "400: Bad request"
def calculate(expression: str):
for i in expression:
if i not in '.0123456789+-*$':
return "400: Bad request"
if '$' in expression:
index1 = expression.index('$')
left_index = index1 - 1
while left_index >= 0:
if expression[left_index].isdigit() or expression[left_index] == '.':
left_index -= 1
else:
break
left_index += 1
right_index = index1 + 1
while right_index < len(expression):
if expression[right_index].isdigit() or expression[right_index] == '.':
right_index += 1
else:
break
right_index -= 1
expression = expression[:left_index] + str(
float(expression[left_index:index1]) / float(expression[index1 + 1:right_index + 1])) + expression[
right_index + 1:]
if '*' in expression:
index2 = expression.index('*')
left_index = index2 - 1
while left_index >= 0:
if expression[left_index].isdigit() or expression[left_index] == '.':
left_index -= 1
else:
break
left_index += 1
right_index = index2 + 1
while right_index < len(expression):
if expression[right_index].isdigit() or expression[right_index] == '.':
right_index += 1
else:
break
right_index -= 1
expression = expression[:left_index] + str(
float(expression[left_index:index2]) * float(expression[index2 + 1:right_index + 1])) + expression[
right_index + 1:]
if '-' in expression:
index3 = expression.index('-')
left_index = index3 - 1
while left_index >= 0:
if expression[left_index].isdigit() or expression[left_index] == '.':
left_index -= 1
else:
break
left_index += 1
right_index = index3 + 1
while right_index < len(expression):
if expression[right_index].isdigit() or expression[right_index] == '.':
right_index += 1
else:
break
right_index -= 1
expression = expression[:left_index] + str(
float(expression[left_index:index3]) - float(expression[index3 + 1:right_index + 1])) + expression[
right_index + 1:]
if '+' in expression:
index4 = expression.index('+')
left_index = index4 - 1
while left_index >= 0:
if expression[left_index].isdigit() or expression[left_index] == '.':
left_index -= 1
else:
break
left_index += 1
right_index = index4 + 1
while right_index < len(expression):
if expression[right_index].isdigit() or expression[right_index] == '.':
right_index += 1
else:
break
right_index -= 1
expression = expression[:left_index] + str(
float(expression[left_index:index4]) + float(expression[index4 + 1:right_index + 1])) + expression[
right_index + 1:]
return float(expression) if int(str(expression).split('.')[1]) != 0 else int(float(expression))
assert calculate("1.1") == 1.1
assert calculate("10+5") == 15
assert calculate("8-2") == 6
assert calculate("4*3") == 12
assert calculate("18$2") == 9
assert calculate("5+8-8*2$4") == 9
assert calculate("3x+1") == "400: Bad request"