【Codewars】Sudoku Solution Validator 实现数独验证器

本博客介绍如何编写一个Python函数validSolution,用于验证数独解决方案的正确性。函数接受一个9x9的二维数组,其中0表示空格,并检查行、列和3x3宫格是否符合数独规则。提供了三种不同的实现方法,包括使用sorted()函数、通过列表推导式检查重复元素以及使用set()进行去重。

原题描述Description:


Sudoku Background

Sudoku is a game played on a 9x9 grid. The goal of the game is to fill all cells of the grid with digits from 1 to 9, so that each column, each row, and each of the nine 3x3 sub-grids (also known as blocks) contain all of the digits from 1 to 9. 
(More info at: http://en.wikipedia.org/wiki/Sudoku)

Sudoku Solution Validator

Write a function validSolution that accepts a 2D array representing a Sudoku board, and returns true if it is a valid solution, or false otherwise. The cells of the sudoku board may also contain 0's, which will represent empty cells. Boards containing one or more zeroes are considered to be invalid solutions.

题意就是让同学们做一个数独验证器,能够检测输入的一个简单二位数组(board)是否是一个已经完成(没有0,0表示未完成的空格)且正确(符合数独规则,即横竖、9个小宫格里面都有且只有1-9)


这道题目从大方向上来说应该用验证性的方法,即有错就return False,排错到最后return True


下面是几种思路:

1.活用sorted()函数可以免去排序的麻烦

correct = [1, 2, 3, 4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值