본문 바로가기

코딩테스트 준비/[백준]

[백준] 10845 - 큐 (파이썬)

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

 

10845번: 큐

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

www.acmicpc.net

 

import sys
N = int(sys.stdin.readline())
commands = []
[commands.append(sys.stdin.readline().split()) for _ in range(N)]

queue = []
for command in commands:
    if command[0] == 'push':
        queue.append(int(command[1]))
    elif command[0] == 'pop':
        if queue:
            print(queue.pop(0))
        else:
            print(-1)
    elif command[0] == 'size':
        print(len(queue))
    elif command[0] == 'empty':
        if queue:
            print(0)
        else:
            print(1)
    elif command[0] == 'front':   
        if queue:
            print(queue[0])
        else:
            print(-1)
    else: # back  
        if queue:
            print(queue[-1])
        else:
            print(-1)