"""Offline Python algorithm lab for Data Structures Chapter 01 (tutorial)."""
from collections import deque
import heapq
import json
VERSION = "Windows x64 CPython 3.12.14 offline DSA lab"
def case_basic_operations_crud_sort():
records = [
{"id": 103, "score": 82},
{"id": 101, "score": 95},
{"id": 104, "score": 76},
]
# 插入
records.append({"id": 102, "score": 88})
# 检索
found = next((r for r in records if r["id"] == 101), None)
# 更新
for r in records:
if r["id"] == 104:
r["score"] = 90
# 删除
records = [r for r in records if r["id"] != 103]
# 排序(按 score 升序)
records.sort(key=lambda r: r["score"])
expected = {
"found_score": 95,
"after_ops_ids": [102, 104, 101],
"after_ops_scores": [88, 90, 95],
"operation_count": 5,
}
actual = {
"found_score": found["score"] if found else None,
"after_ops_ids": [r["id"] for r in records],
"after_ops_scores": [r["score"] for r in records],
"operation_count": 5,
}
return {
"name": "basic_operations_crud_sort",
"summary": "验证数据结构上的检索、插入、删除、更新与排序五大常用运算",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_linear_structures_overview():
# 数组:按索引直接访问
arr = [10, 20, 30, 40]
arr_elem = arr[2]
# 链表:通过 next 引用顺序遍历
class Node:
def __init__(self, val, nxt=None):
self.val = val
self.next = nxt
head = Node("A", Node("B", Node("C")))
linked_vals = []
cur = head
while cur:
linked_vals.append(cur.val)
cur = cur.next
# 栈:LIFO
stack = []
for x in (1, 2, 3):
stack.append(x)
stack_pop_order = [stack.pop(), stack.pop(), stack.pop()]
# 队列:FIFO
q = deque()
for x in (1, 2, 3):
q.append(x)
queue_pop_order = [q.popleft(), q.popleft(), q.popleft()]
expected = {
"array_index_2": 30,
"linked_list": ["A", "B", "C"],
"stack_lifo": [3, 2, 1],
"queue_fifo": [1, 2, 3],
}
actual = {
"array_index_2": arr_elem,
"linked_list": linked_vals,
"stack_lifo": stack_pop_order,
"queue_fifo": queue_pop_order,
}
return {
"name": "linear_structures_overview",
"summary": "验证数组、链表、栈与队列四类常见线性数据结构的核心访问顺序",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_nonlinear_and_hash_overview():
# 树:最小示例二叉树节点数与边数
tree = {"root": ["left", "right"], "left": ["leaf1"], "right": [], "leaf1": []}
tree_nodes = len(tree)
tree_edges = sum(len(ch) for ch in tree.values())
# 图:无向邻接表顶点与边数
graph = {"A": ["B", "C"], "B": ["A", "C"], "C": ["A", "B"]}
graph_vertices = len(graph)
graph_undirected_edges = sum(len(nb) for nb in graph.values()) // 2
# 堆:最小二叉堆弹出顺序
heap_data = [42, 15, 29, 8, 23]
heapq.heapify(heap_data)
heap_pop_two = [heapq.heappop(heap_data), heapq.heappop(heap_data)]
# 散列表:F(T) 直接定位
slots = [None] * 7
key = 25
slot_idx = key % 7
slots[slot_idx] = ("record-25", key)
expected = {
"tree_nodes_edges": [4, 3],
"graph_v_e": [3, 3],
"min_heap_top2": [8, 15],
"hash_slot_idx": 4,
"hash_direct_hit": "record-25",
}
actual = {
"tree_nodes_edges": [tree_nodes, tree_edges],
"graph_v_e": [graph_vertices, graph_undirected_edges],
"min_heap_top2": heap_pop_two,
"hash_slot_idx": slot_idx,
"hash_direct_hit": slots[slot_idx][0],
}
return {
"name": "nonlinear_and_hash_overview",
"summary": "验证树、图、二叉堆与散列表四类非线性/散列结构的基本组织与定位行为",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_language_and_terminology_audit():
pillars = ["logical_structure", "storage_structure", "operations"]
terminology = {
"data": "信息的载体",
"data_element": "数据的基本单位",
"data_structure": "带特定关系的数据元素集合",
"algorithm": "有限步骤解决问题的明确指令",
}
refs = [
"https://docs.python.org/3/tutorial/datastructures.html",
"https://github.com/krahets/hello-algo",
"https://github.com/TheAlgorithms/Python",
"https://github.com/liuyubobobo/Play-with-Algorithms",
]
expected = {
"pillar_count": 3,
"term_count": 4,
"recommended_lang": "Python",
"all_refs_https": True,
}
actual = {
"pillar_count": len(pillars),
"term_count": len(terminology),
"recommended_lang": "Python",
"all_refs_https": all(u.startswith("https://") for u in refs),
}
return {
"name": "language_and_terminology_audit",
"summary": "校验数据结构三要素、基础术语映射与权威开源学习参考链接",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_basic_operations_crud_sort(),
case_linear_structures_overview(),
case_nonlinear_and_hash_overview(),
case_language_and_terminology_audit(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 本地离线算法与数据结构实测环境(标准库确定性脚本)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 本地离线算法与数据结构实测环境(标准库确定性脚本)。
入口文件:projects/data-structures-tutorial-lab/explore-tutorial.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 本地离线算法与数据结构实测环境(标准库确定性脚本)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联44章) · 含当前章节或实例 · mbskycom202612a2641.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2677.zip