What is the mistake in this Sudoku checker code?

In summary: False # make sure that all sudoku inputs are integers, else break out with False if (entry in memory_list) and (len(memory_list)!=0): return False #repeated integer in row memory_list.append(entry)
  • #1
doktorwho
181
6

Homework Statement


Python:
# Sudoku [[PLAIN]http://en.wikipedia.org/wiki/Sudoku][/PLAIN] 
# is a logic puzzle where a game
# is defined by a partially filled
# 9 x 9 square of digits where each square
# contains one of the digits 1,2,3,4,5,6,7,8,9.
# For this question we will generalize
# and simplify the game.

# Define a procedure, check_sudoku,
# that takes as input a square list
# of lists representing an n x n
# sudoku puzzle solution and returns the boolean
# True if the input is a valid
# sudoku square and returns the boolean False
# otherwise.

# A valid sudoku square satisfies these
# two properties:

#   1. Each column of the square contains
#       each of the whole numbers from 1 to n exactly once.

#   2. Each row of the square contains each
#       of the whole numbers from 1 to n exactly once.

# You may assume the the input is square and contains at
# least one row and column.

correct = [[1,2,3],
           [2,3,1],
           [3,1,2]]

incorrect = [[1,2,3,4],
             [2,3,1,3],
             [3,1,2,3],
             [4,4,4,4]]

incorrect2 = [[1,2,3,4],
             [2,3,1,4],
             [4,1,2,3],
             [3,4,1,2]]

incorrect3 = [[1,2,3,4,5],
              [2,3,1,5,6],
              [4,5,2,1,3],
              [3,4,5,2,1],
              [5,6,4,3,2]]

incorrect4 = [['a','b','c'],
              ['b','c','a'],
              ['c','a','b']]

incorrect5 = [ [1, 1.5],
               [1.5, 1]]
             
def check_sudoku(p):
    rows=[]
    coloums=[]
    t=0
    for i in p:
        for e in i:
            if e not in rows:
                rows.append(e)
            else:
                return False
        rows=[]
    while t<=len(p):
        for i in p:
            if i.pop() not in coloums:
                coloums.append(i.pop())
            else:
                return False
        coloums=[]
        t += 1
    return True
 
print check_sudoku(incorrect)
#>>> False

print check_sudoku(correct)
#>>> True

print check_sudoku(incorrect2)
#>>> False

print check_sudoku(incorrect3)
#>>> False

print check_sudoku(incorrect4)
#>>> False

print check_sudoku(incorrect5)
#>>> False

Homework Equations


3. The Attempt at a Solution [/B]
I don't actually want you to correct me, i just want for you to point out where i made the mistake as my code output gives all as False. I was sure it correctly checked everything, i have no idea why its behaving this way. The first part must be correct but the second might be the problem. Do you see the mistake?
 
Last edited by a moderator:
  • Like
Likes berkeman
Technology news on Phys.org
  • #2
I don't think your nested loop is doing what you think. Try putting a print statement after the "for e in i:" line that prints out e and rows. I think you will quickly see the problem.
 
  • #3
phyzguy said:
I don't think your nested loop is doing what you think. Try putting a print statement after the "for e in i:" line that prints out e and rows. I think you will quickly see the problem.
I exchanged what should be rows and what coloums, for me the rows are the elements in a sublists and the coloums are respective elements in each list. When i delete the second part of the code and check just the rows everything is ok. I even messed up something to know ots false and it gives it as false. The problem is in the second part. After the while statement. I can't see what it is.
 
  • #4
Got it :D
 
  • #5
What about this one? (since you found your answer)
Python:
def checker( board ):
    for row in board:
        memory_list=[]
        for entry in row:
            if not isinstance(entry, int) or (entry>len(board)) : return False # make sure that all sudoku inputs are integers, else break out with False
            if (entry in memory_list) and (len(memory_list)!=0): return False #repeated integer in row
            memory_list.append(entry)
    for iCol in range(len(board)):
        memory_list=[]
        Column= [row[iCol] for row in board]
        for entry in Column:
            if (entry in memory_list) and (len(memory_list)!=0): return False
            memory_list.append(entry)
    return True
 

1. What are common causes of errors in Python code?

There are a few common causes of errors in Python code, such as syntax errors, logical errors, and runtime errors. Syntax errors occur when the code does not follow the correct syntax rules, while logical errors occur when the code does not produce the desired output. Runtime errors, also known as exceptions, occur when the code attempts to perform an invalid operation or encounters unexpected data.

2. How do I debug my Python code?

To debug your Python code, you can use a debugger, which allows you to step through your code and analyze its execution. You can also use print statements to track the values of variables and identify where the code is going wrong. Additionally, you can use tools like PyCharm or Visual Studio Code, which have built-in debugging features.

3. What is the best way to handle errors in Python?

The best way to handle errors in Python is to use try-except blocks. This allows you to catch and handle specific errors that may occur in your code. You can also use the raise statement to create custom exceptions and handle them accordingly. Another approach is to use logging to track errors and provide useful information for debugging.

4. How can I optimize my Python code for better performance?

To optimize your Python code for better performance, you can use tools like profiling to identify bottlenecks in your code and make necessary improvements. Additionally, you can use built-in data structures and functions that are optimized for performance, such as dictionaries and list comprehensions. Writing efficient algorithms and avoiding unnecessary operations can also improve performance.

5. Is there a difference between Python 2 and Python 3?

Yes, there are several differences between Python 2 and Python 3, including changes in syntax, standard library functions, and the way strings and Unicode are handled. Python 3 also has some new features, such as the print function, that are not available in Python 2. It is important to note that Python 2 is no longer supported, so it is recommended to use Python 3 for new projects.

Similar threads

  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
10
Views
724
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
947
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top