백준 10866 파이썬 (덱)

철웅·2022년 10월 8일
0

BOJ

목록 보기
8/46

문제 : https://www.acmicpc.net/problem/10866


💻 Code

import sys
input = sys.stdin.readline
from collections import deque

n = int(input())
result = deque()

for i in range(n):
    command = input().split()
    if(command[0] == 'push_front'):
        result.appendleft(command[1])
    elif(command[0] == 'push_back'):
        result.append(command[1])
    elif(command[0] == 'pop_front'):
        try:
            print(result.popleft())
        except:
            print(-1)
    elif(command[0] == 'pop_back'):
        try:
            print(result.pop())
        except:
            print(-1)
    elif(command[0] == 'size'):
        print(len(result))
    elif(command[0] == 'empty'):
        if(len(result)==0):
            print(1)
        else:
            print(0)
    elif(command[0] == 'front'):
        try:
            print(result[0])
        except:
            print(-1)
    elif(command[0] == 'back'):
        try:
            print(result[-1])
        except:
            print(-1)

같은 패턴 문제 연속으로 풀어서 지겹긴 하지만 deque랑 try문이나 쓰려고 풀어봤다.

0개의 댓글