-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Received possibly erroneous FAIL message! #57
Comments
Issues in the Code: Handling Zeros: Misleading Logic for Zero Handling: #Explanation of the Corrected Code:
#Row Check Logic:
#Zeros Handling: 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:
#Explanation of the Corrected Code: #Row Check Logic: #Zeros Handling: |
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.
#Explanation of the Corrected Code:
#Counter Initialization:
- counter = [0] * 9: Initializes a list to keep count of occurrences of numbers 1 to 9.
#Row Check Logic:
- 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 Handling:
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
#Explanation of the Corrected Code:
#Counter Initialization:
counter = [0] * 9: Initializes a list to keep count of occurrences of numbers 1 to 9.
#Row Check Logic:
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 Handling:
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.
The text was updated successfully, but these errors were encountered: