728x90
반응형
Python
import sys
stack = []
for _ in range(int(sys.stdin.readline())):
command = sys.stdin.readline().split()
if command[0] == "push":
stack.append(int(command[1]))
elif command[0] == "top":
if stack == []:
print(-1)
else:
print(stack[-1])
elif command[0] == "size":
print(len(stack))
elif command[0] == "empty":
if stack==[]:
print(1)
else:
print(0)
elif command[0] == "pop":
if stack == []:
print(-1)
else:
print(stack[-1])
stack.pop()
import sys
stack = []
for _ in range(int(sys.stdin.readline())):
command = sys.stdin.readline().split()
if command[0] == "push":
stack.append(int(command[1]))
elif command[0] == "top":
print( -1 if stack == [] else stack[-1])
elif command[0] == "size":
print(len(stack))
elif command[0] == "empty":
print( 1 if stack == [] else 0)
elif command[0] == "pop":
if stack == []:
print(-1)
else:
print(stack[-1])
stack.pop()
Python
import sys
q = []
for _ in range(int(sys.stdin.readline())):
command = sys.stdin.readline().split()
if command[0] == "push":
q.append(int(command[1]))
elif command[0] == "pop":
if q == []:
print(-1)
else:
print(q[0])
del q[0]
elif command[0] == "size":
print(len(q))
elif command[0] == "empty":
print(1 if q==[] else 0)
elif command[0] == "front":
print(q[0] if q!=[] else -1)
elif command[0] == "back":
print(q[-1] if q!=[] else -1)
Python
import sys
dq = []
for _ in range(int(sys.stdin.readline())):
cmd = sys.stdin.readline().rstrip()
#print(cmd,dq)
if cmd == "pop_front":
if len(dq)==0: print(-1)
else: print(dq.pop(0))
elif cmd == "pop_back":
print(-1 if len(dq)==0 else dq.pop() )
elif cmd == "size":
print(len(dq))
elif cmd == "empty":
print(1 if len(dq)==0 else 0)
elif cmd == "front":
print(-1 if len(dq)==0 else dq[0] )
elif cmd == "back":
print(-1 if len(dq)==0 else dq[-1] )
else:
cmd = cmd.split()
if cmd[0] == "push_front":
dq.insert(0, int(cmd[1]))
elif cmd[0] == "push_back":
dq.append(int(cmd[1])
728x90
반응형
'Algorithm Problems' 카테고리의 다른 글
[백준] [Python] 12789번 도키도키 간식드리미 (0) | 2021.04.25 |
---|---|
[백준] [Python] 10773번 제로 - 스택 (0) | 2021.04.24 |
[Cos Pro 1급] 4차 9번 시침분침 각도 (0) | 2021.04.18 |
[Cos Pro 2급] 4차 9번 - 위험지역 (0) | 2021.04.10 |
[정올] [Python] 3519번 병합정렬 (0) | 2021.04.03 |