Skip to main content
Code Review

Return to Question

Added link to programming challenge, edited tags, reformatted question a bit.
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

Valid Sudoku in Swift, howThis is my solution to make it shorter in syntax and keep intuitive , like the bottom Python code?

I am handling Valid SudokuLeetCode – Valid Sudoku in Leetcode. I use Swift.

here is my code:

Here isHow can I make it shorter in syntax and keep intuitive, like the following Python code:?

Valid Sudoku in Swift, how to make it shorter in syntax and keep intuitive , like the bottom Python code?

I am handling Valid Sudoku in Leetcode. I use Swift.

here is my code:

Here is the Python code:

This is my solution to LeetCode – Valid Sudoku in Swift.

How can I make it shorter in syntax and keep intuitive, like the following Python code?

Source Link
dengApro
  • 421
  • 4
  • 17

Valid Sudoku in Swift

Valid Sudoku in Swift, how to make it shorter in syntax and keep intuitive , like the bottom Python code?

I am handling Valid Sudoku in Leetcode. I use Swift.

img

here is my code:

class Solution {
 func isValidSudoku(_ board: [[Character]]) -> Bool {
 let count = board.count
 
 
 var set = Set<Character>()
 for i in 0..<count{
 
 // firstly
 set = Set(board[i])
 
 var num = board[i].reduce(0 , {(result : Int, char : Character)
 in
 var cent = 0
 if String(char) == "."{
 cent = 1
 }
 return result + cent
 })
 
 if num > 0 , count - num != set.count - 1{
 return false
 }
 else if num == 0, set.count != count{
 return false
 }
 
 
 // secondly
 
 set = Set(board.reduce([Character]() , { resultArray, chars in
 return resultArray + [chars[i]]
 }))
 
 num = board.reduce(0 , {(result : Int, chars : [Character])
 in
 var cent = 0
 if String(chars[i]) == "."{
 cent = 1
 }
 return result + cent
 })
 
 if num > 0 , count - num != set.count - 1{
 return false
 }
 else if num == 0, set.count != count{
 return false
 }
 
 
 
 // thirdly
 let characters = board.flatMap{
 return 0ドル
 }
 
 
 let fisrtMiddle = ( i/3 ) * 27 + ( i % 3 ) * 3 + 1
 let secondMiddle = fisrtMiddle + 9
 let thirdMiddle = fisrtMiddle + 18
 let arrayThree = [characters[fisrtMiddle - 1], characters[fisrtMiddle], characters[fisrtMiddle + 1],
 characters[secondMiddle - 1], characters[secondMiddle], characters[secondMiddle + 1],
 characters[thirdMiddle - 1], characters[thirdMiddle], characters[thirdMiddle + 1]]
 set = Set(arrayThree)
 num = arrayThree.reduce(0 , {(result : Int, char : Character)
 in
 var cent = 0
 if String(char) == "."{
 cent = 1
 }
 return result + cent
 })
 
 if num > 0 , count - num != set.count - 1{
 return false
 }
 else if num == 0, set.count != count{
 return false
 }
 }
 
 
 return true
 }
}

Here is the Python code:

def isValidSudoku(self, board):
 seen = sum(([(c, i), (j, c), (i/3, j/3, c)]
 for i, row in enumerate(board)
 for j, c in enumerate(row)
 if c != '.'), [])
 return len(seen) == len(set(seen))

The Python code is very Pythonic and short.

How to use Swift syntax power to make my code shorter?

I think Functional is a good choice. RxSwift is welcomed.

default

AltStyle によって変換されたページ (->オリジナル) /