[큐]-10845번
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
My answer
import sys
n=int(sys.stdin.readline())
tmp=[]
for _ in range(n):
que=[]
que=sys.stdin.readline().split()
if(que[0]=="push"):
tmp.append(int(que[1]))
elif(que[0]=="front"):
if(tmp==[]):print(-1)
else:print(tmp[0])
elif(que[0]=="back"):
if(tmp==[]):print(-1)
else:print(tmp[-1])
elif(que[0]=="size"):
print(len(tmp))
elif(que[0]=="pop"):
if(tmp==[]):print(-1)
else:
print(tmp[0])
tmp=tmp[1:]
elif(que[0]=="empty"):
if(tmp):print(0)
else:print(1)
Another answer
q,m=[],[]
p=m.append
for i in range(int(input())):
a=input().split();c=a[0][-1]
if c=='h':q+=[a[1]]
if c=="p":p(q.pop(0)if q else -1)
if c=='e':p(len(q))
if c=='y':p((bool(q))^1)
if c=='t':p(q[0]if q else-1)
if c=='k':p(q[-1]if q else-1)
print(*m,sep="\n")
더보기
내 코드는 그냥 정직하게 알아볼 수 있게 쓴 것 같고 아래 코드는 간단화하려고 입력받는단어의 마지막글자를 인식해서 풀었다. 나머지는 똑같은데 각각 안에 식을 좀 더 단순하게 써서 좋아보인다.
728x90
'코딩테스트 > 백준[Python]' 카테고리의 다른 글
| [Python/백준] #10866- [덱] (0) | 2021.12.01 |
|---|---|
| [Python/백준] #10799 - [쇠막대기] [try_again] (0) | 2021.11.30 |
| [Python/백준] #9012- [괄호] (0) | 2021.11.30 |
| [Python/백준] #18870 - [좌표 압축] [try_again] (0) | 2021.11.30 |
| [Python/백준] #10828- [스택] (0) | 2021.11.28 |