虽然脱离了码农行列,但是觉得写代码这个技能,每个人都应该会一点,而且最近突然有兴趣学习起python,恰好朋友提到codewar以打怪练级的形式来刷题熟练技能,于是就去玩了,感觉这种方式确实比较有成就感,而且不同等级对应不同难度,刚好适合我这种新手学习。为了能有所成长,就决定写写笔记,把想法和心得记录一番,希望能坚持和有所提高(虽然之前的学习计划都坑掉了。。
目前的等级是6(数字越低等级越高,最初是8级),抽到的题目难度基本在区间6和5之间,这次遇到的是难度5的题目
Write a function done_or_not passing a board (list[list_lines]) as parameter. If the board is valid return 'Finished!', otherwise return 'Try again!'
Sudoku rules:
Complete the Sudoku puzzle so that each and every row, column, and region contains the numbers one through nine only once.
Rows:
There are 9 rows in a traditional Sudoku puzzle. Every row must contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. There may not be any duplicate numbers in any row. In other words, there can not be any rows that are identical.
In the illustration the numbers 5, 3, 1, and 2 are the "givens". They can not be changed. The remaining numbers in black are the numbers that you fill in to complete the row.
Columns:
There are 9 columns in a traditional Sudoku puzzle. Like the Sudoku rule for rows, every column must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Again, there may not be any duplicate numbers in any column. Each column will be unique as a result.
In the illustration the numbers 7, 2, and 6 are the "givens". They can not be changed. You fill in the remaining numbers as shown in black to complete the column.
Regions
A region is a 3x3 box like the one shown to the left. There are 9 regions in a traditional Sudoku puzzle.
Like the Sudoku requirements for rows and columns, every region must also contain the numbers 1, 2, 3, 4, 5, 6, 7, 8, and 9. Duplicate numbers are not permitted in any region. Each region will differ from the other regions.
In the illustration the numbers 1, 2, and 8 are the "givens". They can not be changed. Fill in the remaining numbers as shown in black to complete the region.
Valid board example:
For those who don't know the game, here are some information about rules and how to play Sudoku:http://en.wikipedia.org/wiki/Sudoku andhttp://www.sudokuessentials.com/
其实就是数独的判断,输入一个矩阵,判断这个矩阵是否已经完成“数独”(数独的规则就是横列,纵列,以及各个9宫格的数字都为1-9且没有重复)
横列很好判断,直接可以使用set()函数取集合,若集合的长度为9,便可判定为“数独”。
纵列的话,就取每个横列的某列元素重组成一个列表再进行判断。
九宫格比较麻烦,首先想到的就是通过循环逐个取数,然后3个数一循环来取数,虽然比较笨的方法,但是实现起来最好思考。
def done_or_not(board):
for list in board:
if len(set(list)) < 9:
return 'Try again!'
board_rv = [ [row[i] for row in board] for i in range(len(board))]
for list in board_rv:
if len(set(list)) < 9:
return 'Try again!'
list_k = []
for q in range(0,7,3):
for j in range(0,7,3):
for k in range(q,q+3):
for i in range(j,j+3):
list_k.append(board[k][i])
if len(set(list_k)) < 9:
return 'Try again!'
del list_k[:]
return 'Finished!'
虽然通过了测试,不过自己也感觉这个方法比较蠢。
后来看到别人做的用切片方式,更简洁一些,自己也尝试着稍微做了修改,看执行时间上效率确实提高了不少,codewar的好处是每次做完题能看到别人的方法,每次都会有惊喜,能学到不少技巧
def done_or_not(board):
for list in board:
if len(set(list)) <> 9:
return 'Try again!'
board_rv = [ [row[i] for row in board] for i in range(len(board))]
for list in board_rv:
if len(set(list)) <> 9:
return 'Try again!'
list_b = [board[i][j:j+3] + board[i+1][j:j+3] + board[i+2][j:j+3] for i in range(0,7,3) for j in range(0,7,3)]
for list in list_b:
if len(set(list)) <> 9:
return 'Try again!'
return 'Finished!'