結果

問題 No.49 算数の宿題
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-02-26 15:10:34
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 735 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 10,828 KB
実行使用メモリ 8,664 KB
最終ジャッジ日時 2023-08-24 17:29:34
合計ジャッジ時間 1,065 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,488 KB
testcase_01 AC 17 ms
8,560 KB
testcase_02 AC 17 ms
8,480 KB
testcase_03 AC 18 ms
8,664 KB
testcase_04 AC 18 ms
8,520 KB
testcase_05 AC 18 ms
8,480 KB
testcase_06 AC 18 ms
8,440 KB
testcase_07 AC 18 ms
8,488 KB
testcase_08 AC 17 ms
8,612 KB
testcase_09 AC 17 ms
8,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import collections
import itertools


def calc(equation):
    is_operator = lambda x: x in "+*"
    operators = collections.deque()
    operands = collections.deque()
    for key, group in itertools.groupby(equation, is_operator):
        if key:
            operators.append("*" if "".join(group) == "+" else "+")
        else:
            operands.append(int("".join(group)))
    value = operands.popleft()
    while operators and operands:
        operand = operands.popleft()
        if operators.popleft() == "+":
            value += operand
        else:
            value *= operand
    return value


def main():
    print(calc(input()))


if __name__ == '__main__':
    main()
0