GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - eastyou/combinatorics: 初中数学排列组合工具:排列数组合数、结果枚举、反求 n 与练习纸

BranchesTags
Open more actions menu

Latest commit

 

History

1 Commit
1 Commit

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
combinatorics
combinatorics
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
pyproject.toml
pyproject.toml
 
 

Repository files navigation

combinatorics

CI License: MIT Python

简介

combinatorics 是一个面向初中数学教学的零依赖命令行工具,用于练习排列与组合:计算排列数 P(n, k)、组合数 C(n, k),用字母枚举全部结果,已知结果反求元素个数 n,并生成可打印的 A4 练习纸与公式卡片页。

所有计算只依赖 Python 标准库,安装后既可在终端使用,也可作为库导入。

特性

  • 零第三方依赖,仅使用 Python 标准库。
  • 提供 perm、comb、list、solve、sheet、demo 六个子命令。
  • 排列数与组合数使用整数精确计算,n 最大 1000,k 最大 26。
  • 枚举使用 A 到 Z 共 26 个字母,结果超过 10000 项时给出明确错误。
  • 严格校验外部输入:布尔值、NaN、Inf、小数、空文本与越界值都会被拒绝。
  • 练习题按种子生成,相同种子得到完全相同的题目,可选择性附带答案页。
  • 全部核心函数带类型注解,兼容 Python 3.9 及以上版本。

安装

从源码目录安装:

git clone https://github.com/eastyou/combinatorics.git
cd combinatorics
python3 -m pip install .

也可以不安装,直接在仓库根目录以模块方式运行:

python3 -m combinatorics --help

使用

计算排列数:

$ python3 -m combinatorics perm --n 5 --k 2
排列数:20
计算过程:P(5, 2) = 5 × 4 = 20

计算组合数:

$ python3 -m combinatorics comb --n 5 --k 2
组合数:10
计算过程:C(5, 2) = (5 × 4) ÷ (2 × 1) = 10

枚举全部排列,每行一个结果:

$ python3 -m combinatorics list --n 3 --k 2 --mode perm
AB
AC
BA
BC
CA
CB

枚举全部组合:

$ python3 -m combinatorics list --n 3 --k 2 --mode comb
AB
AC
BC

已知组合数反求 n:

$ python3 -m combinatorics solve --total 15 --k 2 --mode comb
目标:C(n, 2) = 15
解得:n = 6

无解时返回退出码 2,并在标准错误输出提示:

$ python3 -m combinatorics solve --total 60 --k 2 --mode comb
错误:在 n = 0 到 60 范围内无解:C(n, 2) = 60

生成练习纸(默认 8 题,--answers 附带答案页):

$ python3 -m combinatorics sheet --count 8 --seed 1 --answers -o sheet.html
已生成练习纸:sheet.html(8 题,含答案)

生成公式与例题卡片页:

$ python3 -m combinatorics demo -o demo.html
已生成演示页:demo.html

参数

子命令 参数 是否必填 说明
perm --n 是 元素总数 n,0 到 1000 的整数
perm --k 是 取出个数 k,0 到 26 的整数,且不能大于 n
comb --n 是 元素总数 n,0 到 1000 的整数
comb --k 是 取出个数 k,0 到 26 的整数,且不能大于 n
list --n 是 可用字母个数 n,0 到 26 的整数
list --k 是 每次取出个数 k,0 到 26 的整数,且不能大于 n
list --mode 是 枚举方式:perm 排列、comb 组合
solve --total 是 已知的排列数或组合数,需不小于 1
solve --k 是 取出个数 k,0 到 26 的整数
solve --mode 是 反求方式:perm 排列、comb 组合
sheet --count 否 题目数量,1 到 50,默认 8
sheet --seed 否 随机种子,默认 1,相同种子结果可复现
sheet --answers 否 附带参考答案页
sheet -o / --output 是 输出 HTML 文件路径
demo -o / --output 是 输出 HTML 文件路径

list 的枚举结果按字母顺序依次生成;当 k 为 0 时只有一种取法,输出一行「(空)」。solve 在 n = 0 到 60 的范围内查找,找不到时按无解处理。

退出码

退出码 含义
0 执行成功,结果已输出或文件已写入
2 输入非法、参数缺失、无解或文件写入失败

错误信息统一以「错误:」开头写入标准错误,正常结果写入标准输出。

作为库使用

from combinatorics import (
    combinations_count,
    enumerate_items,
    make_questions,
    permutations_count,
    render_sheet,
    solve_n,
)

print(permutations_count(5, 2))          # 20
print(combinations_count(5, 2))          # 10
print(enumerate_items(2, "comb", 3))     # [('A', 'B'), ('A', 'C'), ('B', 'C')]
print(solve_n(15, 2, "comb"))            # 6

questions = make_questions(count=8, seed=1)
document = render_sheet(questions, answers=True)
with open("sheet.html", "w", encoding="utf-8") as handle:
    handle.write(document)

核心函数在输入非法时抛出中文 ValueError,调用方可据此给出提示。

开发与测试

python3 -m unittest discover -s tests -v

测试共 153 个用例,覆盖排列数、组合数、结果枚举与反求的正常值和边界值,练习题种子复现,HTML 能否被标准库 html.parser 解析,每个子命令的成功与失败路径,以及命令行退出码。测试产物一律写入临时目录,不留在仓库中。

项目要求 Python 3.9 及以上,语法保持 3.9 兼容;已在 Python 3.9.6 与 3.15.2 上验证用例数一致且全部通过。

License

MIT © 2026 eastyou

About

初中数学排列组合工具:排列数组合数、结果枚举、反求 n 与练习纸

Topics

clieducationmathpythonteaching-tools

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.