【每日一题20220330】点号计算器

:man_mage: 给定一个由点号 . 和算术运算符组成的字符串,请编写一个函数,计算它们所对应数字算式的结果。计算规则只需要支持四种即可:加法(“+”)、减法(“-”)、乘法(“*”)、整除(“//”)。

示例:
输入: "..... - ..."
输出: ".."
解释:该算式翻译成数字为 5-3,所以结果是2,再翻译成点号为"…"

题目难度:简单
题目来源:CodeWars:Dot Calculator

def solution(operation: str) -> str:
    # your code here

assert solution("..... + ...............") == "...................."
assert solution("..... - ...") == ".."
assert solution("..... * ...") == "..............."
assert solution("..... // ..") == ".."
def calculator(txt: str):
    res = txt.split(' ')
    return '.'*eval(f'{len(res[0])}{res[1]}{len(res[2])}')

def solution(a: str):
res = a.split(" “)
print(str(len(res[0])) + res[1] + str(len(res[2])))
return eval(str(len(res[0])) + res[1] + str(len(res[2])))"

print(solution("… * …"))

def solution(operation: str) -> str:
    [num_left, opr, num_right] = operation.split(' ')
    return '.' * eval(f"{len(num_left)}{opr}{len(num_right)}")
def solution(operation: str) -> str:
    return '.'*eval(f"{len(operation.split(' ')[0])}{operation.split(' ')[1]}{len(operation.split(' ')[2])}")


assert solution("..... + ...............") == "...................."
assert solution("..... - ...") == ".."
assert solution("..... * ...") == "..............."
assert solution("..... // ..") == ".."

在此处键入或粘贴代码

def solution(operation):
    lefts, opers, rights = operation.split()
    left_len, right_len = len(lefts), len(rights)
    result = '.' * eval(f'{left_len} {opers} {right_len}')
    return result

def solution(txt: str):
    res = txt.split(' ')
    return '.'*eval(f'{len(res[0])}{res[1]}{len(res[2])}')

image

def solution(operation: str) -> str:
    x, op, y = operation.split(' ')
    a = x.count('.')
    b = y.count('.')
    return '.' * eval(f'{a}{op}{b}')

assert solution("..... + ...............") == "...................."
assert solution("..... - ...") == ".."
assert solution("..... * ...") == "..............."
assert solution("..... // ..") == ".."

def solution(operation: str) -> str:
    l1 = operation.split(' ')
    return eval(str(len(l1[0]))+l1[1]+str(len(l1[2])))*'.'
def solution(operation: str) -> str:
    if '+' in operation:
        operation_list=operation.split("+")
        nums = operation_list[0].count('.')+operation_list[1].count('.')
        return ''.join(['.']*nums)
    if '-' in operation:
        operation_list=operation.split("-")
        nums = operation_list[0].count('.')-operation_list[1].count('.')
        return ''.join(['.']*nums)
    if '*' in operation:
        operation_list=operation.split("*")
        nums = operation_list[0].count('.')*operation_list[1].count('.')
        return ''.join(['.']*nums)
    if '//' in operation:
        operation_list=operation.split("//")
        nums = operation_list[0].count('.')//operation_list[1].count('.')
        return ''.join(['.']*nums)

assert solution("..... + ...............") == "...................."
assert solution("..... - ...") == ".."
assert solution("..... * ...") == "..............."
assert solution("..... // ..") == ".."