1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A problem with python code

  1. Dec 19, 2016 #1
    1. The problem statement, all variables and given/known data
    Code (Python):

    # Sudoku [[PLAIN]http://en.wikipedia.org/wiki/Sudoku][/PLAIN] [Broken]
    # 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],

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

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

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

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

    incorrect5 = [ [1, 1.5],
                   [1.5, 1]]
    def check_sudoku(p):
        for i in p:
            for e in i:
                if e not in rows:
                    return False
        while t<=len(p):
            for i in p:
                if i.pop() not in coloums:
                    return False
            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
    2. Relevant equations
    3. The attempt at a solution

    I dont 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: May 8, 2017
  2. jcsd
  3. Dec 19, 2016 #2


    User Avatar
    Science Advisor

    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.
  4. Dec 20, 2016 #3
    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 cant see what it is.
  5. Dec 20, 2016 #4
    Got it :D
  6. Dec 20, 2016 #5


    User Avatar
    Gold Member

    What about this one? (since you found your answer)
    Code (Python):

    def checker( board ):
        for row in board:
            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
        for iCol in range(len(board)):
            Column= [row[iCol] for row in board]
            for entry in Column:
                if (entry in memory_list) and (len(memory_list)!=0): return False
        return True
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: A problem with python code
  1. Matlab code to Python (Replies: 1)