Skip to main content
Code Review

Return to Answer

added 2 characters in body; added 30 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18

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 :

added 591 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18

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
 }
}
deleted 1030 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18

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 7s, 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 7s, 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))
deleted 30 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18
Loading
added 1104 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18
Loading
added 1768 characters in body
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18
Loading
Source Link
ielyamani
  • 889
  • 1
  • 5
  • 18
Loading
default

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /