Insights Blog
-- Browse All Articles --
Physics Articles
Physics Tutorials
Physics Guides
Physics FAQ
Math Articles
Math Tutorials
Math Guides
Math FAQ
Education Articles
Education Guides
Bio/Chem Articles
Technology Guides
Computer Science Tutorials
Forums
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Trending
Featured Threads
Log in
Register
What's new
Search
Search
Search titles only
By:
Chemistry
Biology and Medical
Earth Sciences
Computer Science
Computing and Technology
DIY Projects
Menu
Log in
Register
Navigation
More options
Contact us
Close Menu
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Other Sciences
Programming and Computer Science
What is the mistake in this Sudoku checker code?
Reply to thread
Message
[QUOTE="doktorwho, post: 5647669, member: 605653"] [h2]Homework Statement [/h2] [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], [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 [/code] [h2]Homework Equations[/h2] 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? [/QUOTE]
Insert quotes…
Post reply
Forums
Other Sciences
Programming and Computer Science
What is the mistake in this Sudoku checker code?
Back
Top