코딩테스트 치트시트(Python)

이태혁·2020년 12월 18일
0

🦊 최소 힙

import heapq

def heapsort(iterable):
	h = []
	result = []
	for value in iterable:
		heapq.heappush(h, value)
	for i in range(len(h)):
		result.append(heapq.heappop(h))
	return result

result = heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
print(result)
#결과
#>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

🦊 스택

stack = []

stack.append(5)
stack.append(2)
stack.pop()

print(stack[::-1])
print(stack)

🦊 큐

from collections import deque
queue = deque()

queue.append(5)
queue.append(2)
queue.popleft()
print(queue)
queue.reverse()
print(queue)
profile
back-end, cloud, docker, web의 관심이 있는 예비개발자입니다.

0개의 댓글