본문 바로가기

Dev/BAEKJOON 백준

[백준/BOJ] 백준 코딩 알고리즘 1935번/Python

728x90
반응형

 백준 코딩 알고리즘 문제 1935

 

백준 코딩 알고리즘 문제 1935번 풀이 - 사용 언어: Python

# Practive 002 : Postfix
# https://boj.kr/1935

N = int(input())
input_str = input()

operand_stk = []

# save operand data to each chr
operand_data = dict()
for i in range(N):
	operand_data[chr(65+i)] = int(input())

# calculate input
for c in input_str:
	if 65 <= ord(c) <= 90:  # save operand in stack
		operand_stk.append(c)
	else:  # when cursor meet operator
		result = 0

		num_type_list = [int, float, complex]
		if type(operand_stk[-1]) in num_type_list:
			x2 = operand_stk.pop()
		else:
			x2 = operand_data[operand_stk.pop()]

		if type(operand_stk[-1]) in num_type_list:
			x1 = operand_stk.pop()
		else:
			x1 = operand_data[operand_stk.pop()]

		if c == '+':
			result = x1 + x2
		elif c == '-':
			result = x1 - x2
		elif c == '*':
			result = x1 * x2
		elif c == '/':
			result = x1 / x2

		operand_stk.append(result)

print("{:.2f}".format(operand_stk.pop()))
728x90
반응형