Python Forum
Sudoku making with base Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sudoku making with base Python
#1
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)
Larz60+ write Oct-17-2023, 05:49 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
I modified your code this time. Please use BBCode tags on future posts.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to get the sudoku solver to work? Tool1211 8 3,159 Feb-03-2022, 04:49 PM
Last Post: BashBedlam
  Code by Voice using Python ( Need HELP in making ADVANCE ) pawandeore 0 1,444 Feb-24-2021, 06:59 PM
Last Post: pawandeore

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020