MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / generate_board

Function generate_board

projects/Sudoku_solver/main.py:4–41  ·  view source on GitHub ↗
(num)

Source from the content-addressed store, hash-verified

2
3
4def generate_board(num):
5 base = 3
6 side = base * base
7
8 def pattern(r, c):
9 return (base * (r % base) + r // base + c) % side
10
11 def shuffle(s):
12 return sample(s, len(s))
13
14 # randomize rows, col, num
15 rBase = range(base)
16 rows = [g * base + r for g in shuffle(rBase) for r in shuffle(rBase)]
17 cols = [g * base + c for g in shuffle(rBase) for c in shuffle(rBase)]
18 nums = shuffle(range(1, base * base + 1))
19
20 # randomized baseline
21 board_tmp = [[nums[pattern(r, c)] for c in cols] for r in rows]
22
23 # print full board
24 print("=======full board========")
25 print_board(board_tmp)
26
27 # remove numbers of the board
28 squares = side * side
29 if num == 0:
30 # default number of empty slots
31 empties = squares * 3 // 4
32 else:
33 # given number of empty slots
34 empties = 81 - num
35 # looping a randomized board for the amount of empty mubers
36 for p in sample(range(squares), empties):
37 # set nubers to 0 of the randomized board
38 board_tmp[p // side][p % side] = 0
39
40 # returning the generated board
41 return board_tmp
42
43
44"""

Callers 1

main.pyFile · 0.70

Calls 3

shuffleFunction · 0.70
patternFunction · 0.70
print_boardFunction · 0.70

Tested by

no test coverage detected