For ordered processing: A* search, Dijkstra, event simulation, task scheduling. Efficient min/max extraction with heap-based queue.
Use heapq for O(log n) push/pop of minimum element.
import heapq
# Basic usage
heap = []
heapq.heappush(heap, 3)
heapq.heappush(heap, 1)
heapq.heappush(heap, 2)
heapq.heappop(heap) # Returns 1 (minimum)
# With tuples for priority ordering
tasks = []
heapq.heappush(tasks, (priority, task_id, task_data))
_, _, task = heapq.heappop(tasks)
# heapify existing list
data = [3, 1, 4, 1, 5]
heapq.heapify(data) # In-place, O(n)
import heapq
class PriorityQueue:
"""A queue where the item with minimum key is always popped first."""
def __init__(self, items=(), key=lambda x: x):
self.key = key
self.items = [] # Heap of (score, item) pairs
for item in items:
self.add(item)
def add(self, item):
"""Add item to the queue."""
pair = (self.key(item), item)
heapq.heappush(self.items, pair)
def pop(self):
"""Pop and return the item with minimum key."""
return heapq.heappop(self.items)[1]
def top(self):
"""Peek at minimum item without removing."""
return self.items[0][1]
def __len__(self):
return len(self.items)
# Usage in A* search
def astar_search(problem, h):
frontier = PriorityQueue([Node(problem.initial)],
key=lambda n: n.path_cost + h(n))
while frontier:
node = frontier.pop()
if problem.is_goal(node.state):
return node
for child in expand(problem, node):
frontier.add(child)
return None
(priority, tiebreaker, data) for stable orderingnpx skills add jimmc414/build-priority-queue下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer