Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 16th, 2013, 10:19 AM
 Akshat1
Contributing User

Join Date: Feb 2013
Posts: 92
Time spent in forums: 22 h 38 m 33 sec
Reputation Power: 1
Help with a problem?

Here's the problem:
Code:
# 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.
# example of an input -
#"incorrect = [[1,2,3,4],
#            [2,3,1,3],
#           [3,1,2,3],
#           [4,4,4,4]]"

Now, I've thought about breaking this problem down to two parts -- 1st will check for rows, the 2nd will check for columns.
EDIT: The first one is done, I need a solution for the second one(columns).
Also, if you have a better approach to solve the problem, then you are most welcome.

Last edited by Akshat1 : June 16th, 2013 at 10:21 AM.

#2
June 16th, 2013, 10:29 AM
 Akshat1
Contributing User

Join Date: Feb 2013
Posts: 92
Time spent in forums: 22 h 38 m 33 sec
Reputation Power: 1
Also, the code I used for rows is this:
Code:
def row_check(x):
for ealst in x:
first = [ealst[0]]
for each in ealst[1:]:
if each not in first:
first.append(each)
else:
return False
return True

The whole code is written by me, except the last return statement. The only thing I am not understanding is that when the last return statement returns true and is independent of any condition(s), why doesn't the code always return True regardless of whether the input is correct or not?

Last edited by Akshat1 : June 16th, 2013 at 10:31 AM.

#3
June 16th, 2013, 01:28 PM
 dwblas
Contributing User

Join Date: May 2009
Posts: 421
Time spent in forums: 5 Days 7 h 39 m 40 sec
Reputation Power: 32
The "return "False" is executed first.
Code:
def testit(value_in):
if 0 == value_in:
return False
return True

for val in [0, 1, 0, 2, 3]:
print val, testit(val)

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Help with a problem?