본문 바로가기
CT

[Python] 백준 28279번 : 덱 2 파이썬

by shur_ 2023. 9. 6.

https://www.acmicpc.net/problem/28279

 

28279번: 덱 2

첫째 줄에 명령의 수 N이 주어진다. (1 ≤ N ≤ 1,000,000) 둘째 줄부터 N개 줄에 명령이 하나씩 주어진다. 출력을 요구하는 명령은 하나 이상 주어진다.

www.acmicpc.net


 


import sys
from collections import deque

N= int(sys.stdin.readline().rstrip())
deq = deque()

for i in range(N):
    
    command = list(map(int, sys.stdin.readline().rstrip().split()))
    
    if command[0] == 1 :
        deq.appendleft(command[1])
        
    elif command[0] == 2 :
        deq.append(command[1])
            
    elif command[0] == 3 :
        print(deq.popleft() if deq else -1)
        
    elif command[0] == 4 :
        print(deq.pop() if deq else -1)
            
    elif command[0] == 5 :
        print(len(deq))
        
    elif command[0] == 6 :
        print(1 if not deq else 0)
            
    elif command[0] == 7 :
        print(deq[0] if deq else -1)
    
    elif command[0] == 8 :
        print(deq[-1] if deq else -1)

 

 

댓글