https://www.acmicpc.net/problem/10828
class stack:
def __init__(self): # stack list 생성성
self.items = []
def push(self, x):
self.items.append(x)
def pop(self):
print(self.items.pop() if len(self.items) != 0 else -1)
def size(self):
print(len(self.items))
def empty(self):
print(0 if len(self.items) == 0 else 1)
def top(self):
print(self.items[-1] if len(self.items) != 0 else -1)
stack = stack()
for _ in range(int(input())):
line = input().split('')
if line[0] == 'push':
stack.push(line[1])
elif line[0] == 'pop':
stack.pop()
elif line[0] == 'size':
stack.size()
elif line[0] == 'empty':
stack.empty()
elif line[0] == 'top':
stack.top()
else:
print('뭐야 잘못들어옴')
import sys
N = int(sys.stdin.readline())
stack = []
for _ in range(N):
line = sys.stdin.readline().split()
if line[0] == 'push':
stack.append(line[1])
elif line[0] == 'pop':
print(stack.pop() if len(stack) != 0 else -1)
elif line[0] == 'size':
print(len(stack))
elif line[0] == 'empty':
print(0 if stack else 1)
#어떤 분은 print(1 if stack else 0) 이런식으로 함
elif line[0] == 'top':
print(stack[-1] if stack else -1)
else:
print('뭐야 잘못들어옴')
import sys
class stack:
def __init__(self): # stack list 생성성
self.items = []
def push(self, x):
self.items.append(x)
def pop(self):
print(self.items.pop() if len(self.items) != 0 else -1)
def size(self):
print(len(self.items))
def empty(self):
print(1 if len(self.items) == 0 else 0)
def top(self):
print(self.items[-1] if len(self.items) != 0 else -1)
stack = stack()
N = int(sys.stdin.readline())
for _ in range(N):
line = sys.stdin.readline().split()
if line[0] == 'push':
stack.push(line[1])
elif line[0] == 'pop':
stack.pop()
elif line[0] == 'size':
stack.size()
elif line[0] == 'empty':
stack.empty()
elif line[0] == 'top':
stack.top()
else:
print('뭐야 잘못들어옴')
입출력 속도 비교 : sys.stdin.readline > raw_input() > input()