蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 Scala 数组索引统计、二维 ofDim 矩阵、concat/range 与工厂方法

PYTHONWindows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。explore-arrays.py
explore-arrays.py131 行
"""Offline Scala semantic verification lab for Chapter 16 (arrays)."""
import json

VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"


def case_array_declaration_traversal_and_stats():
    z = [None] * 3
    z[0] = "BlueSky"
    z[1] = "Scala"
    z[4 // 2] = "JVM"
    my_list = [1.9, 2.9, 3.4, 3.5]
    total = round(sum(my_list), 4)
    mx = max(my_list)
    expected = {
        "z_elements": ["BlueSky", "Scala", "JVM"],
        "total": 11.7,
        "max_val": 3.5,
        "first_index": 0,
        "last_index": 3,
    }
    actual = {
        "z_elements": z,
        "total": total,
        "max_val": mx,
        "first_index": 0,
        "last_index": len(my_list) - 1,
    }
    return {
        "name": "array_declaration_traversal_and_stats",
        "summary": "验证定长数组索引读写(含表达式下标 4/2)、遍历求和与最大值查找",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_multidimensional_ofdim_matrix():
    matrix = [[0 for _ in range(3)] for _ in range(3)]
    for i in range(3):
        for j in range(3):
            matrix[i][j] = j
    expected = {"rows": 3, "cols": 3, "matrix": [[0, 1, 2], [0, 1, 2], [0, 1, 2]]}
    actual = {"rows": len(matrix), "cols": len(matrix[0]), "matrix": matrix}
    return {
        "name": "multidimensional_ofdim_matrix",
        "summary": "验证 Array.ofDim[Int](3, 3) 二维矩阵构建与嵌套循环填充输出",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_concat_and_range_arrays():
    a1 = [1.9, 2.9, 3.4, 3.5]
    a2 = [8.9, 7.9, 0.4, 1.5]
    merged = a1 + a2
    r_step2 = list(range(10, 20, 2))
    r_step1 = list(range(10, 20))
    expected = {
        "merged_len": 8,
        "merged_tail": 1.5,
        "range_step2": [10, 12, 14, 16, 18],
        "range_step1": [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
    }
    actual = {
        "merged_len": len(merged),
        "merged_tail": merged[-1],
        "range_step2": r_step2,
        "range_step1": r_step1,
    }
    return {
        "name": "concat_and_range_arrays",
        "summary": "验证 Array.concat 合并数组与 Array.range 指定步长和默认步长生成区间数组",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_array_companion_factories():
    # Array.iterate(0, 3)(a => a + 1), Array.fill(3)("x"), Array.tabulate(3)(a => a + 5), Array.copy
    iterated = []
    cur = 0
    for _ in range(3):
        iterated.append(cur)
        cur = cur + 1
    filled = ["x" for _ in range(3)]
    tabulated = [i + 5 for i in range(3)]
    tabulated_2d = [[i + j for j in range(2)] for i in range(2)]
    dst = [0, 0, 0, 0]
    src = [10, 20, 30]
    dst[1:3] = src[0:2]
    expected = {
        "empty_len": 0,
        "iterate": [0, 1, 2],
        "fill": ["x", "x", "x"],
        "tabulate_1d": [5, 6, 7],
        "tabulate_2d": [[0, 1], [1, 2]],
        "copied_dst": [0, 10, 20, 0],
    }
    actual = {
        "empty_len": len([]),
        "iterate": iterated,
        "fill": filled,
        "tabulate_1d": tabulated,
        "tabulate_2d": tabulated_2d,
        "copied_dst": dst,
    }
    return {
        "name": "array_companion_factories",
        "summary": "验证 Array 伴生对象工厂方法 empty/iterate/fill/tabulate 与 copy 复制语义",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_array_declaration_traversal_and_stats(),
        case_multidimensional_ofdim_matrix(),
        case_concat_and_range_arrays(),
        case_array_companion_factories(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。

入口文件:projects/scala-arrays-lab/explore-arrays.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-arrays.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Scala

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2408.zip

下载课程关联工程包:Scala

课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2409.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习