結果

問題 No.2595 Parsing Challenge
ユーザー rankturniprankturnip
提出日時 2023-12-24 15:19:37
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,316 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 279,880 KB
最終ジャッジ日時 2023-12-24 15:20:00
合計ジャッジ時間 22,725 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 40 ms
53,460 KB
testcase_02 AC 39 ms
53,460 KB
testcase_03 AC 38 ms
53,460 KB
testcase_04 AC 38 ms
53,460 KB
testcase_05 AC 38 ms
53,460 KB
testcase_06 AC 39 ms
53,460 KB
testcase_07 AC 54 ms
53,460 KB
testcase_08 AC 38 ms
53,460 KB
testcase_09 AC 38 ms
53,460 KB
testcase_10 AC 39 ms
53,460 KB
testcase_11 AC 39 ms
53,460 KB
testcase_12 AC 38 ms
53,460 KB
testcase_13 AC 39 ms
53,460 KB
testcase_14 AC 37 ms
53,460 KB
testcase_15 AC 40 ms
53,460 KB
testcase_16 AC 40 ms
53,460 KB
testcase_17 AC 40 ms
53,460 KB
testcase_18 AC 40 ms
53,460 KB
testcase_19 AC 41 ms
53,460 KB
testcase_20 AC 96 ms
75,888 KB
testcase_21 AC 108 ms
76,216 KB
testcase_22 AC 103 ms
76,028 KB
testcase_23 AC 70 ms
73,916 KB
testcase_24 AC 122 ms
75,668 KB
testcase_25 RE -
testcase_26 AC 506 ms
81,016 KB
testcase_27 AC 342 ms
78,916 KB
testcase_28 AC 450 ms
80,948 KB
testcase_29 AC 260 ms
77,728 KB
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 TLE -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def evaluate_expression(s):
    def parse_nonzero_digit(s, index):
        return int(s[index]), index + 1

    def parse_digit(s, index):
        if s[index] == '0':
            return 0, index + 1
        else:
            return parse_nonzero_digit(s, index)

    def parse_positive_number(s, index):
        if s[index] == '0':
            return parse_digit(s, index)
        else:
            value, next_index = parse_nonzero_digit(s, index)
            while next_index < len(s) and s[next_index].isdigit():
                digit, next_index = parse_digit(s, next_index)
                value = value * 10 + digit
            return value, next_index

    def parse_number(s, index):
        if s[index] == '-':
            value, next_index = parse_number(s, index + 1)
            return -value, next_index
        elif s[index].isdigit():
            return parse_positive_number(s, index)
        else:
            raise ValueError("Invalid number format")

    def parse_factor(s, index):
        if s[index] == '(':
            value, next_index = parse_expression(s, index + 1)
            if s[next_index] == ')':
                return value, next_index + 1
            else:
                raise ValueError("Mismatched parentheses")
        else:
            return parse_number(s, index)

    def parse_term(s, index):
        left, next_index = parse_factor(s, index)
        while next_index < len(s) and (s[next_index] == '*' or s[next_index] == '/'):
            operator = s[next_index]
            right, next_index = parse_factor(s, next_index + 1)
            if operator == '*':
                left *= right
            else:
                raise ValueError("Unsupported operator: '/'")
        return left, next_index

    def parse_expression(s, index):
        left, next_index = parse_term(s, index)
        while next_index < len(s) and (s[next_index] == '+' or s[next_index] == '-'):
            operator = s[next_index]
            right, next_index = parse_term(s, next_index + 1)
            if operator == '+':
                left += right
            else:
                left -= right
        return left, next_index

    result, _ = parse_expression(s, 0)
    return result
n=int(input())
expression = str(input())
result = evaluate_expression(expression)
print(result)
0