Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validate sudoku board function #9881

Merged
merged 19 commits into from
Oct 14, 2023

Conversation

dbring
Copy link
Contributor

@dbring dbring commented Oct 5, 2023

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Oct 5, 2023
@cclauss
Copy link
Member

cclauss commented Oct 6, 2023

mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

matrix/validate_sudoku_board.py:77: error: Need type annotation for "row_values"  [var-annotated]
matrix/validate_sudoku_board.py:78: error: Need type annotation for "col_values"  [var-annotated]
matrix/validate_sudoku_board.py:79: error: Need type annotation for "box_values"  [var-annotated]
matrix/validate_sudoku_board.py:81: error: Incompatible types in assignment (expression has type "int", variable has type "list[str]")  [assignment]
matrix/validate_sudoku_board.py:83: error: No overload variant of "__getitem__" of "list" matches argument type "list[str]"  [call-overload]
matrix/validate_sudoku_board.py:83: note: Possible overload variants:
matrix/validate_sudoku_board.py:83: note:     def __getitem__(self, SupportsIndex, /) -> list[str]
matrix/validate_sudoku_board.py:83: note:     def __getitem__(self, slice, /) -> list[list[str]]
matrix/validate_sudoku_board.py:88: error: Unsupported operand types for // ("list[str]" and "int")  [operator]
Found 6 errors in 1 file (checked 1211 source files)

@dbring
Copy link
Contributor Author

dbring commented Oct 6, 2023

I think we already have this well covered.

https://github.com/TheAlgorithms/Python/blob/master/backtracking/sudoku.py

https://github.com/TheAlgorithms/Python/pulls?q=is%3Apr+sudoku

Yeah, I saw this but I think validating a sudoku board is a materially different task than solving it. Happy to close this if you feel otherwise.

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 6, 2023
@cclauss
Copy link
Member

cclauss commented Oct 6, 2023

You have two different files in this pull request.

@cclauss
Copy link
Member

cclauss commented Oct 6, 2023

Old but nice ideas!! http://norvig.com/sudoku.html

@dbring
Copy link
Contributor Author

dbring commented Oct 8, 2023

@cclauss was there anything else you wanted me to amend?

Comment on lines 24 to 27
NUM_ROWS = 9
NUM_COLS = 9
EMPTY_CELL = "."
IS_VALID = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use NUM_SQUARES instead of NUM_ROWS and NUM_ROWS.
Lets True and False in a boolean function.

Suggested change
NUM_ROWS = 9
NUM_COLS = 9
EMPTY_CELL = "."
IS_VALID = True
NUM_SQUARES = 9
EMPTY_CELL = "."

Comment on lines 70 to 75
if len(sudoku_board) != NUM_ROWS:
raise Exception("Number of rows must be 9.")

for row in sudoku_board:
if len(row) != NUM_COLS:
raise Exception("Number of columns must be 9.")
Copy link
Member

@cclauss cclauss Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(sudoku_board) != NUM_ROWS:
raise Exception("Number of rows must be 9.")
for row in sudoku_board:
if len(row) != NUM_COLS:
raise Exception("Number of columns must be 9.")
if len(sudoku_board) != NUM_SQUARES or any(
len(row) != NUM_SQUARES for row in sudoku_board
):
raise ValueError(f"Sudoku boards must be {NUM_SQUARES}x{NUM_SQUARES} squares.")

@algorithms-keeper algorithms-keeper bot added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Oct 9, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting changes A maintainer has requested changes to this PR label Oct 9, 2023
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 9, 2023
@dbring dbring requested a review from cclauss October 13, 2023 19:01
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 14, 2023
@cclauss cclauss enabled auto-merge (squash) October 14, 2023 15:34
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 14, 2023
@cclauss cclauss merged commit 212cdfe into TheAlgorithms:master Oct 14, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 14, 2023
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* Added algorithm to deeply clone a graph

* Fixed file name and removed a function call

* Removed nested function and fixed class parameter types

* Fixed doctests

* bug fix

* Added class decorator

* Updated doctests and fixed precommit errors

* Cleaned up code

* Simplified doctest

* Added doctests

* Code simplification

* Created function which validates sudoku boards

* Update matrix/validate_sudoku_board.py

* Fixed precommit errors

* Removed file accidentally included

* Improved readability and simplicity

* Add timeit benchmarks

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update validate_sudoku_board.py

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants