This is the question:
Please write a function named row_correct(sudoku: list, row_no: int), which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single row, as its arguments. Rows are indexed from 0.
The function should return True or False, depending on whether the row is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once.
sudoku = [
[9, 0, 0, 0, 8, 0, 3, 0, 0],
[2, 0, 0, 2, 5, 0, 7, 0, 0],
[0, 2, 0, 3, 0, 0, 0, 0, 4],
[2, 9, 4, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 3, 0, 5, 6, 0],
[7, 0, 5, 0, 6, 0, 4, 0, 0],
[0, 0, 7, 8, 0, 3, 9, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 3],
[3, 0, 0, 0, 0, 0, 0, 0, 2]
]
print(row_correct(sudoku, 0)) #True
print(row_correct(sudoku, 1)) #False
the output of the two commands are True and False```
My solution is below:
```Python
# Write your solution here
def row_correct(gameBoard, checkedNum):
counter = 0
if checkedNum == 0:
return True
for row in gameBoard:
for num in row:
if num == checkedNum:
counter += 1
if counter > 1:
return False
return True
if __name__ == "__main__":
sudoku = [
[9, 0, 0, 0, 8, 0, 3, 0, 0],
[2, 0, 0, 2, 5, 0, 7, 0, 0],
[0, 2, 0, 3, 0, 0, 0, 0, 4],
[2, 9, 4, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 7, 3, 0, 5, 6, 0],
[7, 0, 5, 0, 6, 0, 4, 0, 0],
[0, 0, 7, 8, 0, 3, 9, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 3],
[3, 0, 0, 0, 0, 0, 0, 0, 2]
]
print(row_correct(sudoku, 3))
And this was the optimal solution:
def row_correct(sudoku: list, row_no: int):
numbers = []
for number in sudoku[row_no]:
if number > 0 and number in numbers:
return False
numbers.append(number)
return True
The optimal solution looks more cleaner than mine but I am having a bit of trouble understanding it. I thought for number in sudoki[row_no]: would just go through every number in the row and Im not understanding how number in numbers is counting the amount of times number shows up in the row