일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 타입 좁히기
- React
- 무한 스크롤
- SSR
- RTK Query
- 공변성
- 태그된 유니온
- useAppDispatch
- 반공변성
- 투포인터
- 이분 검색
- Promise
- CORS
- map
- Cypress
- CI/CD
- recoil
- ESlint
- app router
- 인터섹션
- dfs
- 호이스팅
- tailwind
- Jest
- async/await
- 결정 알고리즘
- webpack
- TS
- 리터럴 타입
- autosize
Archives
- Today
- Total
짧은코딩
10866 덱 본문
반응형
https://www.acmicpc.net/problem/10866
내 풀이(맞음)
from collections import deque
class Deque:
def __init__(self, ary = deque()):
self.ary = ary
def push_front(self, ary, item):
ary.append(item)
def push_back(self, ary, item):
ary.appendleft(item)
def pop_front(self, ary):
if ary:
t = ary[len(ary)-1]
del ary[len(ary)-1]
return t
else:
return -1
def pop_back(self, ary):
if ary:
t = ary[0]
del ary[0]
return t
else:
return -1
def size(self, ary):
return len(ary)
def empty(self, ary):
if ary:
return 0
else:
return 1
def front(self, ary):
if ary:
return ary[len(ary)-1]
else:
return -1
def back(self, ary):
if ary:
return ary[0]
else:
return -1
a = deque()
d = Deque(a)
n = int(input())
rst = []
for i in range(n):
x = input().split()
if x[0] == 'push_front':
d.push_front(a, x[1])
elif x[0] == 'push_back':
d.push_back(a, x[1])
elif x[0] == 'pop_front':
rst.append(d.pop_front(a))
elif x[0] == 'pop_back':
rst.append(d.pop_back(a))
elif x[0] == 'size':
rst.append(d.size(a))
elif x[0] == 'empty':
rst.append(d.empty(a))
elif x[0] == 'front':
rst.append(d.front(a))
elif x[0] == 'back':
rst.append(d.back(a))
for i in rst:
print(i)
init로 덱인 ary를 만들어줍니다. push_front에서 앞쪽에 삽입하고 push_back에서 뒤쪽에 삽입합니다. 그리고 pop_front에서는 맨 앞쪽걸 삭제하고 출력하고 pop_back에서는 맨 뒤쪽걸 삭제하고 출력합니다. size에선 개수를 출력하고 empty는 비어있으면 1 안비었으면 0을 출력합니다. fornt에서는 맨 앞걸 출력하고 back에서는 맨 뒤에걸 출력합니다.
반응형
'코딩 테스트(Python) > 백준, 프로그래머스' 카테고리의 다른 글
1920 수 찾기 (0) | 2022.02.08 |
---|---|
9012 괄호 (0) | 2022.02.08 |
10845 큐 (0) | 2022.02.07 |
10828 스택 (0) | 2022.02.05 |
16435 스네이크버드 (0) | 2022.02.04 |
Comments