[Python]BaekJoon.AC

[Python]백준 BaekJoon.AC 10866 : 덱(deque, try-except)

스뇨잉 2022. 1. 24. 16:06
728x90
728x90

 

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

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

 

 

10828-스택, 10856-큐, 이번엔 10866-덱

 

스택 기본 문제의 마지막인 듯 하다.

애초에 덱으로 문제를 풀어왔기 때문에 이전과 크게 다르지 않다.

 

명령 가짓수가 많아져 더 추가한 것 말곤 달라진 게 없다.

아래 글을 참고하면 좋겠다.

 

 

 

2022.01.24 - [[Python]BaekJoon.AC] - [Python]백준 BaekJoon.AC 10828 : 스택(deque, try-except)

 

[Python]백준 BaekJoon.AC 10828 : 스택(deque, try-except)

https://www.acmicpc.net/problem/10828 10828번: 스택 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고..

stepping-coding.tistory.com

 

2022.01.24 - [[Python]BaekJoon.AC] - [Python]백준 BaekJoon.AC 10845 : 큐(deque, try-except)

 

[Python]백준 BaekJoon.AC 10845 : 큐(deque, try-except)

https://www.acmicpc.net/problem/10845 10845번: 큐 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 1..

stepping-coding.tistory.com

 

 

 

import sys
from collections import deque

N = int(input())
deq = deque()

for i in range(N):
    string = sys.stdin.readline().split()

    if string[0] == "push_front":
        deq.appendleft(int(string[1]))
    elif string[0] == "push_back":
        deq.append(int(string[1]))
    elif string[0] == "pop_front":
        try:
            print(deq.popleft())
        except:
            print(-1)
    elif string[0] == "pop_back":
        try:
            print(deq.pop())
        except:
            print(-1)
    elif string[0] == "size":
        print(len(deq))
    elif string[0] == "empty":
        if deq:
            print(0)
        else:
            print(1)
    elif string[0] == "front":
        try:
            p = deq.popleft()
            print(p)
            deq.appendleft(p)
        except:
            print(-1)
    elif string[0] == "back":
        try:
            p = deq.pop()
            print(p)
            deq.append(p)
        except:
            print(-1)

 

 

 

728x90
728x90