

from pythonds.basic.stack import Stackclass ArrayStack:
def __init__(self):
self.data = []
def size(self):
return len(self.data)
def isEmpty(self):
return self.size() == 0
def push(self, item):
self.data.append(item)
def pop(self):
return self.data.pop()
def peek(self):
return self.data[-1]
from doublylinkedlist import Node
from doublylinkedlist import DoublyLinkedList
class LinkedListStack:
def __init__(self):
self.data = DoublyLinkedList()
def size(self):
return self.data.getLength()
def isEmpty(self):
return self.size() == 0
def push(self, item):
node = Node(item)
self.data.insertAt(self.size() + 1, node)
def pop(self):
return self.data.popAt(self.size())
def peek(self):
return self.data.getAt(self.size()).data
왜인지 특정 케이스를 통과 못하는 중…
class ArrayStack:
def __init__(self):
self.data = []
def size(self):
return len(self.data)
def isEmpty(self):
return self.size() == 0
def push(self, item):
self.data.append(item)
def pop(self):
return self.data.pop()
def peek(self):
return self.data[-1]
prec = {
'*': 3, '/': 3,
'+': 2, '-': 2,
'(': 1
}
def solution(S):
opStack = ArrayStack()
answer = ''
for c in S:
# 여는 괄호일 때 -> push
if c == '(':
opStack.push(c)
# 닫는 괄호일 때 -> (가 top에 올 때까지 pop
elif c == ')':
while opStack.peek() != '(':
answer += opStack.pop()
# '(' pop
opStack.pop()
# 피연산자일 때 -> 출력
elif c not in prec.keys():
answer += c
# 연산자일 때, 우선순위가 top이 높을 경우 pop, c push
elif not (opStack.isEmpty()) and prec[opStack.peek()] >= prec[c]:
answer += opStack.pop()
opStack.push(c)
# 그 외 -> push()
else:
opStack.push(c)
# 남은 연산자 pop
while not opStack.isEmpty():
answer += opStack.pop()
return answer
class ArrayStack:
def __init__(self):
self.data = []
def size(self):
return len(self.data)
def isEmpty(self):
return self.size() == 0
def push(self, item):
self.data.append(item)
def pop(self):
return self.data.pop()
def peek(self):
return self.data[-1]
def splitTokens(exprStr):
tokens = []
val = 0
valProcessing = False
for c in exprStr:
if c == ' ':
continue
if c in '0123456789':
val = val * 10 + int(c)
valProcessing = True
else:
if valProcessing:
tokens.append(val)
val = 0
valProcessing = False
tokens.append(c)
if valProcessing:
tokens.append(val)
return tokens
def infixToPostfix(tokenList):
prec = {
'*': 3,
'/': 3,
'+': 2,
'-': 2,
'(': 1,
}
opStack = ArrayStack()
postfixList = []
for c in tokenList:
# 여는 괄호일 때 -> push
if c == '(':
opStack.push(c)
# 닫는 괄호일 때 -> (가 top에 올 때까지 pop
elif c == ')':
while opStack.peek() != '(':
postfixList.append(opStack.pop())
# '(' pop
opStack.pop()
# 피연산자일 때 -> 출력
elif c not in prec.keys():
postfixList.append(c)
# 연산자일 때, 우선순위가 top이 높을 경우 pop, c push
elif not (opStack.isEmpty()) and prec[opStack.peek()] >= prec[c]:
postfixList.append(opStack.pop())
opStack.push(c)
# 그 외 -> push()
else:
opStack.push(c)
# 남은 연산자 pop
while not opStack.isEmpty():
postfixList.append(opStack.pop())
return postfixList
# 후위 표현식 계산
def postfixEval(tokenList):
stack = ArrayStack()
for c in tokenList:
# 피연산자(int)면 push
if type(c) == int:
stack.push(c)
else:
a, b = stack.pop(), stack.pop()
if c == '+':
stack.push(b+a)
elif c == '-':
stack.push(b-a)
elif c == '*':
stack.push(b*a)
else:
stack.push(b/a)
return stack.pop()
def solution(expr):
tokens = splitTokens(expr)
postfix = infixToPostfix(tokens)
val = postfixEval(postfix)
return val