Sudoku Checker in Python

2024/10/5 13:20:43

I am trying to create a sudoku checker in python:

ill_formed = [[5,3,4,6,7,8,9,1,2],[6,7,2,1,9,5,3,4,8],[1,9,8,3,4,2,5,6,7],[8,5,9,7,6,1,4,2,3],[4,2,6,8,5,3,7,9],  # <---[7,1,3,9,2,4,8,5,6],[9,6,1,5,3,7,2,8,4],[2,8,7,4,1,9,6,3,5],[3,4,5,2,8,6,1,7,9]]
easy = [[2,9,0,0,0,0,0,7,0],[3,0,6,0,0,8,4,0,0],[8,0,0,0,4,0,0,0,2],[0,2,0,0,3,1,0,0,7],[0,0,0,0,8,0,0,0,0],[1,0,0,9,5,0,0,6,0],[7,0,0,0,9,0,0,0,1],[0,0,1,2,0,0,3,0,6],[0,3,0,0,0,0,0,5,9]]

I am expecting input like that- a list of 9 lists. The zeros represent number that have not been filled in by the user. They can appear multiple times in a row, column or 3x3.

def check_sudoku(grid):
if len(grid) == 9:numsinrow = 0for i in range(9):if len(grid[i]) == 9:numsinrow += 1if numsinrow == 9:for i in range(9):rowoccurence = [0,0,0,0,0,0,0,0,0,0]for j in range(9):rowoccurence[grid[i][j]] += 1temprow = rowoccurence[1:10]if temprow == [1,1,1,1,1,1,1,1,1]:return Trueelse:return Falseelse:return False
else:return False

I obviously need to check that there is a 9x9 list of lists (grid), and that there are no duplicates in each row, column and 3x3 small square. In the code, I first check to see if there are a proper number of rows (There should be 9). Then I check that each row has 9 elements in it (with the ill_formed example you see that this is not the case). I then attempt to check duplicates in each row but I am having some trouble doing so. I thought that I could loop over each row and loop over each element in that row, and add 1 to a list of ints (rowoccurence). For example, if the first number is a 2, then rowoccurence[2] should be equal to 1. The zeros are in rowoccurence[0] and are not checked(I have a temporary list which should take everything except that first element- the zeros- because there could be more than 1 zero in a row and the grid could still be legit). I try to check the temp list (basically rowoccurence) against a reference list of correct values but it does not seem to be working. Could you help me check the rows for duplicates in this sudoku checker? Thank you so much in advance!

Answer

Remember, you're not searching for duplicates -- merely nonzero duplicates. Summing a set works for this. You can also check the legality of the row/column at the same time:

def sudoku_ok(line):return (len(line) == 9 and sum(line) == sum(set(line)))def check_sudoku(grid):bad_rows = [row for row in grid if not sudoku_ok(row)]grid = list(zip(*grid))bad_cols = [col for col in grid if not sudoku_ok(col)]squares = []for i in range(9, step=3):for j in range(9, step=3):square = list(itertools.chain(row[j:j+3] for row in grid[i:i+3]))squares.append(square)bad_squares = [square for square in squares if not sudoku_ok(square)]return not (bad_rows or bad_cols or bad_squares)
https://en.xdnf.cn/q/70480.html

Related Q&A

Subclassing numpy scalar types

Im trying to subclass numpy.complex64 in order to make use of the way numpy stores the data, (contiguous, alternating real and imaginary part) but use my own __add__, __sub__, ... routines.My problem i…

Python file IO w vs wb [duplicate]

This question already has answers here:What does wb mean in this code, using Python?(5 answers)Closed 10 years ago.Wondering what the real difference is when writing files from Python. From what I can…

How to extract hour, minute and second from Series filled with datetime.time values

Data:0 09:30:38 1 13:40:27 2 18:05:24 3 04:58:08 4 09:00:09Essentially what Id like to do is split this into three columns [hour, minute, second]Ive tried the following code but none see…

Getting an error attachment_filename does not exist in my docker environment

Due to some reasons this particular code is not working in docker but it works fine in development environment. I am getting error "TypeError: send_file() got an unexpected keyword argument attach…

How to check if a process with Command line argument is running using python

I would like to check if a script is running with a specific command line argument within a python script.For example I would like to check if:main.py testargIs running. Is there any way I can achieve …

cx_oracle and python 2.7 [duplicate]

This question already has answers here:Python module "cx_Oracle" module could not be found(4 answers)Closed 5 years ago.Im using python 2.7 and cx_oracle ( Windows x86 Installer (Oracle 10g, …

Inheritance in Python C++ extension

I have c++ library that need communicate with Python plugged in modules. Communication supposes implementing by Python some callback c++ interface. I have read already about writing extensions, but no …

exposing C++ class in Python ( only ET_DYN and ET_EXEC can be loaded)

I was looking at here to see how to expose c++ to Python. I have built Python deep learning code which uses boost-python to connect c++ and python and it is running ok, so my system has things for boos…

Migrations in mongoengine: InvalidId

I am working with mongoengine and trying to do a simple migration. I have a field which I would like to migrate from being a StringField to a ReferenceField to another Object. I planned on doing the …

Keras pretrain CNN with TimeDistributed

Here is my problem, I want to use one of the pretrain CNN network in a TimeDistributed layer. But I have some problem to implement it.Here is my model:def bnn_model(max_len):# sequence length and resne…