일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- TS
- useAppDispatch
- 공변성
- 무한 스크롤
- React
- map
- 인터섹션
- Jest
- Promise
- ESlint
- 반공변성
- SSR
- 호이스팅
- app router
- RTK Query
- CORS
- tailwind
- webpack
- 결정 알고리즘
- autosize
- async/await
- 태그된 유니온
- CI/CD
- 이분 검색
- recoil
- dfs
- 리터럴 타입
- 타입 좁히기
- Cypress
- 투포인터
Archives
- Today
- Total
짧은코딩
10828 스택 본문
반응형
https://www.acmicpc.net/problem/10828
내 풀이(맞음)
class Stack:
def __init__(self, ary):
self.ary = []
def push(self, ary, item):
ary.append(item)
def pop(self, ary):
if ary:
t = ary[len(ary)-1]
del ary[len(ary)-1]
return t
else:
return -1
def size(self, ary):
return len(ary)
def empty(self, ary):
if ary:
return 0
else:
return 1
def top(self, ary):
if ary:
return ary[len(ary)-1]
else:
return -1
a = []
rst = []
s = Stack(a)
n = int(input())
for i in range(n):
x = input().split()
if x[0] == 'push':
s.push(a, x[1])
elif x[0] == 'pop':
rst.append(s.pop(a))
elif x[0] == 'size':
rst.append(s.size(a))
elif x[0] == 'empty':
rst.append(s.empty(a))
elif x[0] == 'top':
rst.append(s.top(a))
for i in rst:
print(i)
Stack 클래스를 만들고 __init__로 초기화 함수를 선언했습니다. push는 ary에 추가하는 함수이고 pop은 ary에 내용이 있다면 t에 맨 위 값을 저장하고 삭제해준 다음 t를 리턴합니다. size는 스택의 개수를 구하고 empty는 스택에 값이 존재하면 0 아니면 1을 리턴합니다. 마지막으로 top는 가장 위에 값을 리턴합니다.
반응형
'코딩 테스트(Python) > 백준, 프로그래머스' 카테고리의 다른 글
10866 덱 (0) | 2022.02.07 |
---|---|
10845 큐 (0) | 2022.02.07 |
16435 스네이크버드 (0) | 2022.02.04 |
1744 수 묶기 (0) | 2022.02.03 |
1343 폴리오미노 (0) | 2022.02.02 |
Comments