How Python judges whether Sudoku is legal _python

Source: Internet
Author: User

Introduced

The Sudoku may only be populated with partial numbers, in which the missing digits are used. Said.

Attention matters

A valid Sudoku (only partially populated) is not necessarily solvable. We just need to make the padding space valid.

Thoughts of disintegration

The Sudoku is preprocessed according to rows, columns, and blocks, and then separately judged to be legitimate.
Using Python's expression derivation, anonymous functions and all functions can be easily processed.

Code

Class Solution:

# @param board, a 9x9 2D array
# @return A Boolean
def 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] # using some techniques to store data directly in chunks
 blocks  = [[Blocks_origin[row * 9 + column] 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 if a record (nine digits in one way or another) is legal return all
 ([Check (x) for style in (rows, columns, blocks) for x in style]) # one step down

Summary

Can not use the circulation body as far as possible without circulation body, the above is the entire content of this article, I hope for everyone's study and work can help, if there is doubt you can message exchange.

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.