"""Offline Lua 5.4 semantic verification lab for Chapter 12 (arrays)."""
import json
VERSION = "CPython 3.12.14 offline Lua 5.4 semantic verifier (lua interpreter absent)"
def lua_seq_len(tbl: dict) -> int:
"""Compute Lua border length for a contiguous 1..n sequence."""
n = 0
while (n + 1) in tbl and tbl[n + 1] is not None:
n += 1
return n
def case_one_based_access_and_length():
arr = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50}
words = {1: "Lua", 2: "Tutorial"}
from_zero = [words.get(i, None) for i in range(0, 3)]
expected = {
"idx1_idx3": [10, 30],
"length": 5,
"for_1_to_len": [10, 20, 30, 40, 50],
"zero_based_probe": [None, "Lua", "Tutorial"],
}
actual = {
"idx1_idx3": [arr[1], arr[3]],
"length": lua_seq_len(arr),
"for_1_to_len": [arr[i] for i in range(1, lua_seq_len(arr) + 1)],
"zero_based_probe": from_zero,
}
return {
"name": "one_based_access_and_length",
"summary": "校验 1-based 数组访问、# 长度计算、for 遍历以及访问索引 0 返回 nil",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_negative_index_and_hash_boundary():
arr = {}
for i in range(-2, 3):
arr[i] = i * 2
vals = [arr[i] for i in range(-2, 3)]
# In Lua, # only counts positive contiguous sequence starting at 1 -> [1]=2, [2]=4 => #arr == 2
seq_len = lua_seq_len(arr)
expected = {"neg_to_pos_vals": [-4, -2, 0, 2, 4], "hash_len_counts_1_based_only": 2}
actual = {"neg_to_pos_vals": vals, "hash_len_counts_1_based_only": seq_len}
return {
"name": "negative_index_and_hash_boundary",
"summary": "校验负数与 0 索引可存入 table,且 # 运算符仅统计从 1 开始的连续正整数序列",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_modify_append_and_table_remove():
arr = [10, 20, 30, 40, 50]
arr_mod = list(arr)
arr_mod[1] = 25 # Lua index 2 -> Python index 1
arr_app = list(arr)
arr_app.append(60) # myArray[#myArray + 1] = 60
arr_rem = list(arr)
removed = arr_rem.pop(2) # table.remove(myArray, 3) shifts tail left
expected = {
"modified": [10, 25, 30, 40, 50],
"appended": [10, 20, 30, 40, 50, 60],
"removed_val": 30,
"after_remove": [10, 20, 40, 50],
}
actual = {
"modified": arr_mod,
"appended": arr_app,
"removed_val": removed,
"after_remove": arr_rem,
}
return {
"name": "modify_append_and_table_remove",
"summary": "校验数组元素修改、#arr+1 尾部追加以及 table.remove(arr, 3) 删除并前移元素",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_multidimensional_nested_vs_flat():
nested = {}
for i in range(1, 4):
nested[i] = {}
for j in range(1, 4):
nested[i][j] = i * j
nested_out = [nested[i][j] for i in range(1, 4) for j in range(1, 4)]
flat = {}
max_rows, max_cols = 3, 3
for r in range(1, max_rows + 1):
for c in range(1, max_cols + 1):
flat[r * max_cols + c] = r * c
flat_out = [flat[r * max_cols + c] for r in range(1, max_rows + 1) for c in range(1, max_cols + 1)]
expected = {
"nested_3x3": [1, 2, 3, 2, 4, 6, 3, 6, 9],
"flat_3x3": [1, 2, 3, 2, 4, 6, 3, 6, 9],
"flat_key_count": 9,
}
actual = {"nested_3x3": nested_out, "flat_3x3": flat_out, "flat_key_count": len(flat)}
return {
"name": "multidimensional_nested_vs_flat",
"summary": "校验二维嵌套表数组与一维线性索引映射 row*maxColumns+col 两种多维矩阵实现",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_one_based_access_and_length(),
case_negative_index_and_hash_boundary(),
case_modify_append_and_table_remove(),
case_multidimensional_nested_vs_flat(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Lua 5.4 语义验证器(本机未安装原生 lua/luac 解释器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Lua 5.4 语义验证器(本机未安装原生 lua/luac 解释器)。
入口文件:projects/lua-arrays-lab/explore-arrays.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-arrays.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Lua 5.4 语义验证器(本机未安装原生 lua/luac 解释器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2509.zip
课程关联工程包(登记关联23章) · 含当前章节或实例 · mbskycom202612a2510.zip