Python Forum

Full Version: Sudoku making with base Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good evening,

i am doing some exercises from the course i am attending and one of them is requiring me to realize with base Python a valid sudoku 4x4 grid.
With base python i mean that we cannot use:
1) external lybraries (only the random lybraries)
2) personal functions (we didn't covered them yet)

I have written an example of code that i put here below.
My code have some problems:
1) It doesnt check if we have same number in same column
2) It doesnt check if we have same number in same 2x2 grid
3) I tried to fill the void spaces with "" using multiple while. I think the same output can be done using one for and one while?

Thanks for the help

import random

sudoku = list()
number = list()
difficulty = 7 # max number of elements in sudoku

# Generating a 4x4 table with random number between 1 and 4
for row in range (4):
  column = list()
  for i in range(4):
    column.append(random.randint(1,4))

# Removing duplicates and counting number of elements per row
  sudoku.append(list(set(column)))
  number.append(len(sudoku[row]))

# Counting the total number of elements
elements = sum(number)

# Removing last element from random row until we reach the desired number of elements
while elements > difficulty:
  sudoku[random.randint(1,3)].pop()
  elements = elements - 1

print(sudoku)

# Adding spaces where we have no elements

row_0 = len(sudoku[0])
row_1 = len(sudoku[1])
row_2 = len(sudoku[2])
row_3 = len(sudoku[3])

while row_0 < 4:
  sudoku[0].append("")
  row_0 = len(sudoku[0])

while row_1 < 4:
  sudoku[1].append("")
  row_1 = len(sudoku[1])

while row_2 < 4:
  sudoku[2].append("")
  row_2 = len(sudoku[2])

while row_3 < 4:
  sudoku[3].append("")
  row_3 = len(sudoku[3])

print(sudoku)
randint has no place in your program. A sudoku puzzle does not contain random numbers, the numbers in every puzzle are identical. The difference between puzzles is the position of the numbers. I think random.sample or random.shuffle are better choices for generating random sudoku puzzles. Using randint it is unlikely you will generate a board that has the correct count of each number.

Should the puzzle contain numbers? You aren't doing math. Printing a pretty puzzle may be easier if you use '1', '2', '3' '4' instead of 1, 2, 3, 4. While thinking about that, is there an advantage to making the puzzle a list of rows? If you make the puzzle a flat list, it is simple to generate random puzzles.
puzzle = random.sample('1111222233334444', 16)
This makes checking rows more difficult, but you have not thought of a way to check columns or grids. Maybe rows, columns and grids can all be checked using the same approach. Try thinking about the problem in the abstract instead rows, columns or grids. Imagine how you would validate a puzzle where diagonals and corners had to be unique. When you run into trouble solving a programming problem, try solving a different problem. A different viewpoint may reveal a better approach.