Published:
Updated:


Solution

import collections
import sys
from typing import List


def solution(commands: List[List[str]]) -> List[int]:
    answer = []
    queue = collections.deque()

    for command in commands:
        x1, x2 = '', ''
        if len(command) == 2:
            x1, x2 = command[0], command[1]
        else:
            x1 = command[0]

        my_push(queue, x1, x2)
        my_pop(answer, queue, x1)
        my_size(answer, queue, x1)
        my_empty(answer, queue, x1)
        my_front(answer, queue, x1)
        my_back(answer, queue, x1)

    return answer


def my_back(answer, queue, x1):
    if x1 == 'back':
        if not queue:
            answer.append(-1)
        else:
            answer.append(queue[-1])


def my_front(answer, queue, x1):
    if x1 == 'front':
        if not queue:
            answer.append(-1)
        else:
            answer.append(queue[0])


def my_empty(answer, queue, x1):
    if x1 == 'empty':
        if not queue:
            answer.append(1)
        else:
            answer.append(0)


def my_size(answer, queue, x1):
    if x1 == 'size':
        answer.append(len(queue))


def my_pop(answer, queue, x1):
    if x1 == 'pop':
        if not queue:
            answer.append(-1)
        else:
            popped = queue.popleft()
            answer.append(popped)


def my_push(queue, x1, x2):
    if x1 == 'push':
        queue.append(x2)


N = int(input())
array = []
for _ in range(N):
    array.append(sys.stdin.readline().split())

print(*solution(array), sep='\n')

Leave a comment