1- First off, let's model the Sudoku grid : A row is a set of unique digits from 0 to 9, represented by characters. All theThe rows isall together, will be represented by an array of those sets :
If you're looking for a short, Pythony, solution, (not necessarily the fastest), then here is a solution :
1- First off, let's model the Sudoku grid : A row is a set of unique digits from 0 to 9, represented by characters. All the rows is an array of those sets :
If you're looking for a short, Pythony, solution, (not necessarily the fastest), then here is solution :
1- First off, let's model the Sudoku grid : A row is a set of unique digits from 0 to 9, represented by characters. The rows all together, will be represented by an array of those sets :
If you're looking for a short, Pythony, solution, (not necessarily the fastest), then here is a solution :
Playing golf
If you're looking for a short, Pythony, solution, (not necessarily the fastest), then here is solution :
class Solution {
func isValidSudoku(_ board: [[Character]]) -> Bool {
var seen: [String] = []
for (i, row) in board.enumerated() {
for case let (j, c) in row.enumerated() where c != "." {
seen.append(contentsOf: ["r\(i)\(c)", "c\(j)\(c)", "b\(i/3)\(j/3)\(c)"])
}
}
return seen.count == Set(seen).count
}
}
Playing golf
If you're looking for a short, Pythony, solution, (not necessarily the fastest), then here is solution :
class Solution {
func isValidSudoku(_ board: [[Character]]) -> Bool {
var seen: [String] = []
for (i, row) in board.enumerated() {
for case let (j, c) in row.enumerated() where c != "." {
seen.append(contentsOf: ["r\(i)\(c)", "c\(j)\(c)", "b\(i/3)\(j/3)\(c)"])
}
}
return seen.count == Set(seen).count
}
}
Python
The π solution is wrong. It doesn't check that a 3x3 box is valid. Try it with this input :
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9","7",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
As you can see, the bottom right box contains two 7
s, but the output of the function is True
. Which is incorrect. To fix it, use math.floor
:
import math
def isvalidsudoku(board) -> object:
seen = sum(([(c, i), (j, c), (math.floor(i/3), math.floor(j/3), c)]
for i, row in enumerate(board)
for j, c in enumerate(row)
if c != '.'), [])
return len(seen) == len(set(seen))
Python
The π solution is wrong. It doesn't check that a 3x3 box is valid. Try it with this input :
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9","7",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
As you can see, the bottom right box contains two 7
s, but the output of the function is True
. Which is incorrect. To fix it, use math.floor
:
import math
def isvalidsudoku(board) -> object:
seen = sum(([(c, i), (j, c), (math.floor(i/3), math.floor(j/3), c)]
for i, row in enumerate(board)
for j, c in enumerate(row)
if c != '.'), [])
return len(seen) == len(set(seen))