At codewars kata you can learn programming and practical English at the same time. It is a genius site. If you have a github account, you can get started in 30 seconds If you are interested, start now start here
By the way, you can challenge in many languages other than python.
The assignment is Sudoku (Sudoku), the name of the site is kata (type), the grade notation is kyu (class), this site seems to like Japan By the way, there seems to be a service called Kumite.
Sudoku rules It's a guy who arranges 1 to 9 in vertical, horizontal, 3 * 3 areas.
[[1, 3, 2, 5, 7, 9, 4, 6, 8]
,[4, 9, 8, 2, 6, 1, 3, 7, 5]
,[7, 5, 6, 3, 8, 4, 2, 1, 9]
,[6, 4, 3, 1, 5, 8, 7, 9, 2]
,[5, 2, 1, 7, 9, 3, 8, 4, 6]
,[9, 8, 7, 4, 2, 6, 5, 3, 1]
,[2, 1, 4, 9, 3, 5, 6, 8, 7]
,[3, 6, 5, 8, 1, 7, 9, 2, 4]
,[8, 7, 9, 6, 4, 2, 1, 5, 3]]
agvz
[[1, 3, 2, 5, 7, 9, 4, 6, 8]
,[4, 9, 8, 2, 6, 1, 3, 7, 5]
,[7, 5, 6, 3, 8, 4, 2, 1, 9]
,[6, 4, 3, 1, 5, 8, 7, 9, 2]
,[5, 2, 1, 7, 9, 3, 8, 4, 6]
,[9, 8, 7, 4, 2, 6, 5, 3, 1]
,[2, 1, 4, 9, 3, 5, 6, 8, 7]
,[3, 6, 5, 8, 1, 7, 9, 2, 4]
,[8, 7, 9, 6, 4, 2, 1, 3, 5]]
#5 #3 #When viewed vertically 5,3 is missing
My Answer
import numpy as np
def done_or_not(board):
sets = set([i for i in range(1,10)])
board = np.array(board)
if not [1 for j in range(9) if not set(board[j]) == sets or not set(board[:,j]) == sets] ==[]:
return 'Try again!'
if not [1 for i in range(9) if not set(board[i//3*3:i//3*3+3,i%3*3:(i%3*3)+3].flatten()) == sets ] ==[]:
return 'Try again!'
return 'Finished!'
As sets = {sets of 1-9} Are the columns and columns in this set? Is each area this set? It feels like checking each I feel like the code has become Codewars-like ... I don't know if it's good or bad because it's not readable
Best Answer
import numpy as np
def done_or_not(aboard): #board[i][j]
board = np.array(aboard)
rows = [board[i,:] for i in range(9)]
cols = [board[:,j] for j in range(9)]
sqrs = [board[i:i+3,j:j+3].flatten() for i in [0,3,6] for j in [0,3,6]]
for view in np.vstack((rows,cols,sqrs)):
if len(np.unique(view)) != 9:
return 'Try again!'
return 'Finished!'
What is this ... It's really easy to see Arrange vertical and horizontal squares in a two-dimensional array with np.vstack () You see how many kinds of elements there are in np.unique () I've tried to do the same with len (set ()), but there's such a useful feature I will do my best to write a code that is straightforward and easy to read, rather than being cute.
I started studying CSS and HTML
Recommended Posts