# swea 1223 '계산기2'(전용)

def parse_notation(string):
    op_rank = {
        '+': 1,
        '*': 2
    }

    result = ''
    stk = []
    for c in string:
        if c.isdigit():
            result += c
            continue

        if len(stk) == 0:
            stk.append(c)
            continue

        while len(stk) and op_rank[stk[-1]] >= op_rank[c]:
            result += stk.pop()

        stk.append(c)

    while len(stk):
        result += stk.pop()

    return result


def calculate(string):
    stk = []
    for c in string:
        if c.isdigit():
            stk.append(int(c))
            continue

        num1 = stk.pop()
        num2 = stk.pop()
        if c == '+':
            stk.append(num1 + num2)
        else:
            stk.append(num1 * num2)

    return stk.pop()


if __name__ == '__main__':
    T = 10

    for t in range(1, T+1):
        length = int(input())
        string = input()
        parsed_string = parse_notation(string)
        result = calculate(parsed_string)

        print('#{} {}'.format(t, result))

'알고리즘 문제 풀이 > SWEA' 카테고리의 다른 글

[SWEA 1226 python] 미로  (0) 2021.08.26
[SWEA 1224 python] 계산기3  (0) 2021.08.24
[SWEA 1219 python] 길찾기  (0) 2021.08.19
[SWEA 1234 python] 비밀번호  (0) 2021.08.19
[SWEA 2005 python] 파스칼의 삼각형  (0) 2021.08.18

+ Recent posts