Number fill-in generator in Python: digits behave like words
With ten digits instead of 26 letters, you would expect more numbers that can trade places in a grid. In 500 random grids it hardly mattered: one starting number still left one way to fill 96 to 99 % of them.
A number fill-in is a fill-in puzzle with digits: an empty grid and a list of numbers, grouped by how many digits they have, and the solver fits every number in. No words to know and no sums to work.
The worry with digits is that there are only ten of them, so you might expect more numbers that agree where the grid crosses them, and more grids that fill in two ways. On 31 October 2026 we measured it, with the same grid builder and the same counter as for words.
The short version#
- Easy grids (14 random numbers of 3 to 6 digits): one way to fill with no number printed in place in 76 % of 300. With one starting number, 99 % could be made to fill in one way.
- Medium grids (20 numbers of 3 to 7 digits): 73 % and 96 % of 200.
- For words, in our fill-in post, the same counts were 78 % and 97 % for easy grids and 71 % and 94 % for medium ones, with words of 3 to 7 letters for easy and 3 to 10 for medium. So the digits made no real difference.
- The second way was again two numbers trading places where they cross matching digits, like 722 and 882.
- The generator needed 1.02 sets of numbers a puzzle and 10 to 12 ms.
The code#
The grid comes from best_grid() in our crossword generator post, and the count from fillings() in the fill-in post. What is new is the numbers:
import random
from crossword import best_grid, show # from our crossword generator post
from fill_in import fillings # from our fill-in generator post
def fair_number(length, rng=random):
"""A number of `length` digits that does not start with 0 and is not one digit over and over (like 4444)."""
while True:
n = str(rng.randint(10 ** (length - 1), 10 ** length - 1))
if len(set(n)) > 1:
return n
def numbers(count, shortest, longest, rng=random):
out = set()
while len(out) < count:
out.add(fair_number(rng.randint(shortest, longest), rng))
return sorted(out)
def make(count=14, shortest=3, longest=6, rng=random, tries=100):
"""A grid and a starting number with which it fills in exactly one way; new numbers when no start works."""
for k in range(1, tries + 1):
grid, placed = best_grid(numbers(count, shortest, longest, rng), rng=rng)
for given in rng.sample(range(len(placed)), len(placed)):
if len(fillings(placed, given)) == 1:
return grid, placed, given, k
raise RuntimeError("no grid that fills in one way")grid, placed, given, draws = make(rng=random.Random(3))
print(show(grid))
print("numbers:", len(placed), "| starting number:", placed[given][0], "| ways:", len(fillings(placed, given)),
"| without it:", len(fillings(placed)))With that seed it placed all 14 numbers and picked 7061 to start:
. . . . 9 . . . . . .
. . . . 5 . . 7 . . .
. . 8 7 7 2 5 8 . . .
. . . 5 . . . 7 . . .
. . . 6 . . . . . . .
. 3 7 1 9 5 2 . 7 2 0
9 . . 1 . 1 . . 0 . .
9 . . 5 . 6 . . 6 . .
1 . . . . 4 1 4 1 . .
6 6 7 2 5 2 . 7 . . .
. . . 6 . 5 . 9 . . .
. . . 3 . . . 9 5 7 1This grid would fill in one way even without 7061; about a quarter of easy grids would not.
Why digits do not hurt#
A swap needs two numbers of the same length that agree at every square where the grid crosses them. These grids are sparse: an easy one had 13.2 crossings for 13.9 numbers, so a number crosses about two others and a swap has to agree on only one or two digits, with letters as with digits. The counts barely moved. What does matter is the starting number, exactly as with words: it breaks any swap it touches.
fair_number() keeps two rules a book needs: no number starts with 0, and none is one digit over and over, like 4444.
A whole book, not one grid#
Our number fill in book maker checks every puzzle: a second program, written apart from the one that makes the puzzles, tries every way to fit the listed numbers into the empty grid, starting from the number already in place, and finds exactly one. Every run of two or more boxes is a number from the list, each number is in the grid once, the grid is one connected piece, and the list is grouped by length.
Easy books use numbers of 3 to 6 digits, up to 14 a puzzle; medium 3 to 7 digits, up to 20; hard 4 to 8 digits, up to 26. The number list is set in the grid's own typeface with a slashed zero, and the book is checked against KDP's paperback specs before you can download it.
written
6 min readSamurai sudoku generator in Python: one answer for the board is not enough
A samurai sudoku is five grids that share corner boxes. A generator that only asks for one answer for the whole board left boards where none of the five grids had one answer on its own. Asking it of every grid costs about 14 more starting digits and made the generator 12 times faster.
13 min readKiller sudoku generator in Python: cut the cages until logic solves it
Random cages over a finished grid had exactly one answer in 2 of 60 tries, and logic alone solved none. Cutting cages until logic solves the puzzle works, and the rule of 45 takes it from 218 grids a puzzle to 9.
7 min readSkyscrapers puzzle generator in Python: the edge numbers are rarely enough
All 20 edge numbers of a 5 x 5 skyscrapers grid fix the answer for only 42 % of grids, and only 8 % of our random 6 x 6 grids. A generator then has to print some heights. Printing one where two answers differ needs about a third as many as printing random ones.