|
| 1 | +# Python API |
| 2 | + |
| 3 | +```python |
| 4 | +from sudoku_dlx import ( |
| 5 | + from_string, to_string, is_valid, solve, analyze, rate, |
| 6 | + count_solutions, generate, canonical_form, explain, |
| 7 | + build_reveal_trace, sat_solve |
| 8 | +) |
| 9 | +``` |
| 10 | + |
| 11 | +## Parsing |
| 12 | +```python |
| 13 | +g = from_string("53..7....6..195... ...") # 81 chars; .,0,-,_ are blanks |
| 14 | +s = to_string(g) # always 81 chars with dots for blanks |
| 15 | +``` |
| 16 | + |
| 17 | +## Solve |
| 18 | +```python |
| 19 | +res = solve(g) # None if unsolvable/invalid |
| 20 | +res.grid # 9x9 list of ints |
| 21 | +res.stats.ms, res.stats.nodes, res.stats.backtracks |
| 22 | +``` |
| 23 | + |
| 24 | +## Analyze |
| 25 | +```python |
| 26 | +analyze(g) # dict: {valid, solvable, unique, givens, difficulty, stats{...}} |
| 27 | +``` |
| 28 | + |
| 29 | +## Generate |
| 30 | +```python |
| 31 | +p = generate(seed=123, target_givens=30, minimal=True, symmetry="mix") |
| 32 | +``` |
| 33 | + |
| 34 | +## Canonical form |
| 35 | +```python |
| 36 | +can = canonical_form(g) # 81-char canonical string (isomorphism-invariant) |
| 37 | +``` |
| 38 | + |
| 39 | +## Difficulty |
| 40 | +```python |
| 41 | +score = rate(g) # [0, 10], deterministic (nodes/backtracks/gaps/fill) |
| 42 | +``` |
| 43 | + |
| 44 | +## Explain (human steps) |
| 45 | +```python |
| 46 | +exp = explain(g, max_steps=200) |
| 47 | +exp["steps"] # list of {type, strategy, ...} |
| 48 | +exp["progress"] # 81-char after steps |
| 49 | +exp["solution"] # full solution string (if solvable) |
| 50 | +``` |
| 51 | + |
| 52 | +## Trace (solution reveal) |
| 53 | +```python |
| 54 | +trace = build_reveal_trace(g, res.grid, res.stats) |
| 55 | +# keys: version, kind, initial, solution, steps, stats |
| 56 | +``` |
| 57 | + |
| 58 | +## SAT cross-check (optional) |
| 59 | +```python |
| 60 | +sat = sat_solve(g) # requires python-sat; returns 9x9 grid or None |
| 61 | +``` |
0 commit comments