mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2026-04-16 11:33:34 +00:00
clean up repo.
add more notes
This commit is contained in:
@@ -2,4 +2,4 @@
|
||||
|
||||
- Dijkstra's algorithm works when all weights are non-negative
|
||||
- If there are negative weights use Bellman-Ford.
|
||||
- Priority queue + min heap is optimal when compared to a function that operates on a list.
|
||||
- The book demonstrates a function that operates on a list. Priority queue + min heap added for completeness.
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import heapq
|
||||
|
||||
customers = []
|
||||
heapq.heappush(customers, (2, "Harry"))
|
||||
heapq.heappush(customers, (3, "Charles"))
|
||||
heapq.heappush(customers, (1, "Riya"))
|
||||
heapq.heappush(customers, (4, "Stacy"))
|
||||
|
||||
while customers:
|
||||
print(heapq.heappop(customers))
|
||||
@@ -1,14 +0,0 @@
|
||||
customers = []
|
||||
customers.append((2, "Harry")) # no sort needed here because 1 item.
|
||||
customers.append((3, "Charles"))
|
||||
customers.sort(reverse=True)
|
||||
# Need to sort to maintain order
|
||||
customers.append((1, "Riya"))
|
||||
customers.sort(reverse=True)
|
||||
# Need to sort to maintain order
|
||||
customers.append((4, "Stacy"))
|
||||
customers.sort(reverse=True)
|
||||
|
||||
while customers:
|
||||
print(customers.pop(0))
|
||||
# Will print names in the order: Stacy, Charles, Harry, Riya.
|
||||
@@ -1,12 +0,0 @@
|
||||
from queue import PriorityQueue
|
||||
|
||||
customers = (
|
||||
PriorityQueue()
|
||||
) # we initialise the PQ class instead of using a function to operate upon a list.
|
||||
customers.put((2, "Harry"))
|
||||
customers.put((3, "Charles"))
|
||||
customers.put((1, "Riya"))
|
||||
customers.put((4, "Stacy"))
|
||||
|
||||
while customers:
|
||||
print(customers.get())
|
||||
Reference in New Issue
Block a user