How does Python determine whether a Sudoku is valid?

Source: Internet
Author: User

How does Python determine whether a Sudoku is valid?

Introduction

This Sudoku may only contain some numbers, and the missing numbers are represented.

Notes

A legal Sudoku (partial filling only) is not always solvable. We only need to make the filled space valid.

Disintegration ideas

Pre-process the sudoku by row, column, and block, and then determine whether it is legal.
Using Python expression derivation, anonymous functions andallFunctions can be easily processed.

Code

Class Solution: # @ param board, a 9x9 2D array # @ return a booleandef isValidSudoku (self, board): rows = [list (lst [:]) for lst in board] columns = [[lst [idx] for lst in board] for idx in range (9)] blocks_origin = [board [row] [column] for x in [[0, 1, 2], [3, 4, 5], [6, 7, 8] for y in [[0, 1, 2], [3, 4, 5], [6, 7, 8] for row in x for column in y] # use some techniques to directly store data blocks = [[blocks_origin [row * 9 + c Olumn] for column in range (9)] for row in range (9)] check = lambda lst: all ([lst. count (x) = 1 for x in lst if x! = '. ']) # Determine whether a record (nine numbers arranged in a certain way) is legal return all ([check (x) for style in (rows, columns, blocks) for x in style]) # Step by Step

Summary

If you do not need a circular body, try not to use a circular body. The above is all the content of this article. I hope it will be helpful for your study and work. If you have any questions, please leave a message.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.