[덱]-10866번
My answer
import sys
n=int(sys.stdin.readline())
tmp=[]
for _ in range(n):
deck=[]
deck=sys.stdin.readline().split()
if(deck[0]=="push_front"):
tmp=[int(deck[1])]+tmp
elif(deck[0]=="push_back"):
tmp.append(int(deck[1]))
elif(deck[0]=="front"):
if(tmp==[]):print(-1)
else:print(tmp[0])
elif(deck[0]=="back"):
if(tmp==[]):print(-1)
else:print(tmp[-1])
elif(deck[0]=="size"):
print(len(tmp))
elif(deck[0]=="pop_front"):
if(tmp==[]):print(-1)
else:
print(tmp[0])
tmp=tmp[1:]
elif(deck[0]=="pop_back"):
if(tmp==[]):print(-1)
else:
print(tmp[-1])
tmp=tmp[:-1]
elif(deck[0]=="empty"):
if(tmp):print(0)
else:print(1)
Another answer
n,*l=open(0);a,p=[],print
for i in l:
if"h_f"in(q:=(j:=i.split())[0]):a=[j[1]]+a
elif"h_b"in q:a+=[j[1]]
elif"p_f"in q:p(a.pop(0)if a else-1)
elif"p_b"in q:p(a.pop()if a else-1)
elif"i"in q:p(len(a))
elif"m"in q:p(+(len(a)<1))
elif"r"in q:p(a[0]if a else-1)
else:p(a[len(a)-1]if a else-1)
더보기
저번에 푼 stack,que문제코드로 돌려쓰기 하고있당
728x90
반응형
'코딩테스트 > 백준[Python]' 카테고리의 다른 글
[Python/백준] #10809- [알파벳 찾기] (0) | 2021.12.01 |
---|---|
[Python/백준] #10808 - [알파벳 개수] (0) | 2021.12.01 |
[Python/백준] #10799 - [쇠막대기] [try_again] (0) | 2021.11.30 |
[Python/백준] #10845- [큐] (0) | 2021.11.30 |
[Python/백준] #9012- [괄호] (0) | 2021.11.30 |