mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
fix(curriculum): rename heap module to heapq and correct heap property outputs (#66113)
This commit is contained in:
committed by
GitHub
parent
e796c06e59
commit
0640350046
+5
-2
@@ -98,7 +98,7 @@ The heap property determines the relationship between parent and child nodes. Th
|
||||
- The value of each parent node is less than or equal to the values of its children.
|
||||
- The smallest element is at the root.
|
||||
|
||||
### Python `heap` module example
|
||||
### Python `heapq` module example
|
||||
|
||||
```py
|
||||
import heapq
|
||||
@@ -110,12 +110,15 @@ my_heap = []
|
||||
heapq.heappush(my_heap, 9)
|
||||
heapq.heappush(my_heap, 3)
|
||||
heapq.heappush(my_heap, 5)
|
||||
print(my_heap) # [3, 9, 5]
|
||||
|
||||
# Remove smallest element
|
||||
print(heapq.heappop(my_heap)) # 3
|
||||
print(my_heap) # [5, 9]
|
||||
|
||||
# Push + Pop in one step
|
||||
print(heapq.heappushpop(my_heap, 2)) # 2
|
||||
print(heapq.heappushpop(my_heap, 7)) # 5
|
||||
print(my_heap) # [7, 9]
|
||||
|
||||
# Transform list into heap
|
||||
nums = [5, 7, 3, 1]
|
||||
|
||||
@@ -2823,7 +2823,7 @@ The heap property determines the relationship between parent and child nodes. Th
|
||||
- The value of each parent node is less than or equal to the values of its children.
|
||||
- The smallest element is at the root.
|
||||
|
||||
### Python `heap` module example
|
||||
### Python `heapq` module example
|
||||
|
||||
```py
|
||||
import heapq
|
||||
@@ -2835,12 +2835,15 @@ my_heap = []
|
||||
heapq.heappush(my_heap, 9)
|
||||
heapq.heappush(my_heap, 3)
|
||||
heapq.heappush(my_heap, 5)
|
||||
print(my_heap) # [3, 9, 5]
|
||||
|
||||
# Remove smallest element
|
||||
print(heapq.heappop(my_heap)) # 3
|
||||
print(my_heap) # [5, 9]
|
||||
|
||||
# Push + Pop in one step
|
||||
print(heapq.heappushpop(my_heap, 2)) # 2
|
||||
print(heapq.heappushpop(my_heap, 7)) # 5
|
||||
print(my_heap) # [7, 9]
|
||||
|
||||
# Transform list into heap
|
||||
nums = [5, 7, 3, 1]
|
||||
|
||||
Reference in New Issue
Block a user