import heapq

# 빈 리스트를 만들고 힙으로 사용합니다.
arr = []

patients = [
    ("김철수", 5, 101),
    ("이영희", 9, 102),
    ("박지민", 2, 103),
    ("최승우", 10, 104),
    ("정예지", 7, 105),
    ("강호두", 9, 106)
]

for name,dan,num in patients:
    heapq.heappush(arr, (-dan,name))

top3 = []
for _ in range(3):
    p,n = heapq.heappop(arr)
    print(n)

    
# 데이터를 넣을 때는 heapq.heappush()
# heapq.heappush(numbers, -50)
# heapq.heappush(numbers, -10)
# heapq.heappush(numbers, -30)
# heapq.heappush(numbers, -150)
# heapq.heappush(numbers, -22)
# heapq.heappush(numbers, -35)

# print("현재 힙 상태:", numbers) # 내부적으로는 트리 구조로 저장됩니다.

# # 데이터를 꺼낼 때는 heapq.heappop()
# # 들어간 순서와 상관없이 '가장 작은 값'이 나옵니다.
# print("꺼낸 값:", -heapq.heappop(numbers)) # 10
# print("꺼낸 값:", -heapq.heappop(numbers)) # 30
# print("꺼낸 값:", -heapq.heappop(numbers)) # 50
# print("꺼낸 값:", -heapq.heappop(numbers)) # 10
# print("꺼낸 값:", -heapq.heappop(numbers)) # 30
# print("꺼낸 값:", -heapq.heappop(numbers)) # 50

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: