Open billlocke opened 4 months ago
Issues in the Code: Counter Initialization: The counter list is used to keep track of the number of times each number (1-9) appears in the row. There is an unnecessary increment for zeros which are not part of the numbers 1-9.
Handling Zeros: The code is incorrectly counting zeros and attempting to ensure that zeros appear at most once. In Sudoku, zeros represent empty cells and should be ignored.
Misleading Logic for Zero Handling: The condition else: counter[0] += 1 and if counter[0] > 1 should be removed. Counting zeros is irrelevant for the correctness check of a Sudoku row.
Zeros (representing empty cells) are ignored as they do not affect the correctness of the row in terms of containing numbers 1 to 9 at most once.
Corrected Code:
# Function to check if a row in Sudoku is correct
def row_correct(sudoku, row_no):
counter = [0] * 9 # counters for values 1 to 9 in Sudoku
for square in sudoku[row_no]:
if square > 0:
counter[square - 1] += 1
if counter[square - 1] > 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],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[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, 5)) # Should return True as this row is correct
sudoku = [
[9, 0, 0, 0, 8, 0, 3, 0, 0], # row 0
[2, 2, 0, 0, 5, 0, 7, 0, 0], # row 1
[0, 2, 0, 3, 0, 0, 4, 0, 4], # row 2
[2, 9, 4, 0, 0, 0, 0, 0, 0], # row 3
[0, 0, 0, 7, 3, 0, 5, 6, 0], # row 4
[7, 0, 5, 0, 6, 0, 4, 0, 0], # row 5
[0, 0, 7, 8, 0, 3, 9, 6, 6], # row 6
[3, 0, 1, 0, 0, 0, 0, 0, 3], # row 7
[3, 0, 0, 0, 2, 0, 2, 0, 1], # row 8
]
print(row_correct(sudoku, 0)) # Should return True as this row is correct
counter = [0] * 9: Initializes a list to keep count of occurrences of numbers 1 to 9.
For each number in the specified row (sudoku[row_no]), if the number is greater than 0, increment the corresponding counter. Check if any number appears more than once using if counter[square - 1] > 1.
Zeros (representing empty cells) are ignored as they do not affect the correctness of the row in terms of containing numbers 1 to 9 at most once.
Hello - thank you for the response. Your solution is elegant. I am beginning to glean what Pythonic means - I hope to code in that manner some day. Where I went wrong was my interpretation of the instructions:
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.
I took the words "contains each" to mean that we had to have every number from 1 to 9, but each no more than once. The code I originally wrote looked for all the number from 1 through 9, Thanks again. I'm getting a lot from this course. On Sunday, July 28, 2024 at 12:46:56 PM EDT, Yellecode @.***> wrote:
Issues in the Code: Counter Initialization: The counter list is used to keep track of the number of times each number (1-9) appears in the row. There is an unnecessary increment for zeros which are not part of the numbers 1-9.
Handling Zeros: The code is incorrectly counting zeros and attempting to ensure that zeros appear at most once. In Sudoku, zeros represent empty cells and should be ignored.
Misleading Logic for Zero Handling: The condition else: counter[0] += 1 and if counter[0] > 1 should be removed. Counting zeros is irrelevant for the correctness check of a Sudoku row.
Zeros (representing empty cells) are ignored as they do not affect the correctness of the row in terms of containing numbers 1 to 9 at most once.
Corrected Code:
def row_correct(sudoku, row_no): counter = [0] * 9 # counters for values 1 to 9 in Sudoku for square in sudoku[row_no]: if square > 0: counter[square - 1] += 1 if counter[square - 1] > 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],
[1, 2, 3, 4, 5, 6, 7, 8, 9],
[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, 5)) # Should return True as this row is correct
sudoku = [
[9, 0, 0, 0, 8, 0, 3, 0, 0], # row 0
[2, 2, 0, 0, 5, 0, 7, 0, 0], # row 1
[0, 2, 0, 3, 0, 0, 4, 0, 4], # row 2
[2, 9, 4, 0, 0, 0, 0, 0, 0], # row 3
[0, 0, 0, 7, 3, 0, 5, 6, 0], # row 4
[7, 0, 5, 0, 6, 0, 4, 0, 0], # row 5
[0, 0, 7, 8, 0, 3, 9, 6, 6], # row 6
[3, 0, 1, 0, 0, 0, 0, 0, 3], # row 7
[3, 0, 0, 0, 2, 0, 2, 0, 1], # row 8
]
print(row_correct(sudoku, 0)) # Should return True as this row is correct
counter = [0] * 9: Initializes a list to keep count of occurrences of numbers 1 to 9.
For each number in the specified row (sudoku[row_no]), if the number is greater than 0, increment the corresponding counter. Check if any number appears more than once using if counter[square - 1] > 1.
Zeros (representing empty cells) are ignored as they do not affect the correctness of the row in terms of containing numbers 1 to 9 at most once.
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>
https://tmc.mooc.fi/paste/FP72RASLqTUHcFy8Uzu3-A
Received FAIL message on the 2nd Sudoku. I cannot figure out what the issue is. HELP.