Skip to content
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

Open
billlocke opened this issue Jul 26, 2024 · 2 comments
Open

Received possibly erroneous FAIL message! #57

billlocke opened this issue Jul 26, 2024 · 2 comments

Comments

@billlocke
Copy link

https://tmc.mooc.fi/paste/FP72RASLqTUHcFy8Uzu3-A

Received FAIL message on the 2nd Sudoku. I cannot figure out what the issue is. HELP.

@Yellecode
Copy link

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.

@billlocke
Copy link
Author

billlocke commented Aug 6, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants