[BOJ] 10866. 덱

Jimeaning·2023년 4월 4일
0

코딩테스트

목록 보기
52/143

Python3

문제

입출력

입출력 예시

주요 포인트

덱을 학습할 수 있는 문제였다.

  1. push_front 일 때, appendleft() 함수 사용
  2. push_back 일 때, append() 사용
  3. pop_front일 때, popleft() 사용
  4. pop_back일 때, pop() 사용
  5. size일 때, 덱의 길이 출력
  6. empty일 때, 덱의 길이가 0이면 1 출력
  7. front일 때, 덱의 인덱스 0값 출력
  8. back일 때, 덱의 인덱스 마지막 값 출력

최종 코드

from collections import deque
import sys

n = int(sys.stdin.readline())
deq = deque()

for i in range(n):
    mth = sys.stdin.readline().split()
    
    if mth[0] == 'push_front':
        deq.appendleft(mth[1])
    elif mth[0] == 'push_back':    
        deq.append(mth[1])
    elif mth[0] == 'pop_front':
        if len(deq) == 0:
            print(-1)
        else: print(deq.popleft())
    elif mth[0] == 'pop_back':    
        if len(deq) == 0:
            print(-1)
        else: print(deq.pop())
    elif mth[0] == 'size':
        print(len(deq))
    elif mth[0] == 'empty':
        if len(deq) == 0:
            print(1)
        else: print(0)
    elif mth[0] == 'front':
        if len(deq) == 0:
            print(-1)
        else: print(deq[0])
    elif mth[0] == 'back':
        if len(deq) == 0:
            print(-1)
        else: print(deq[-1])

피드백

덱은 양쪽 끝에서 삽입, 삭제가 가능하다.

push_front일 때
appendleft() 함수를 이용

pop_front일 때
popleft() 함수를 이용

~left() 함수에 대해 배울 수 있었다.

profile
I mean

0개의 댓글