-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check_sudoku.py
49 lines (42 loc) · 1.41 KB
/
Check_sudoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Sudoku [http://en.wikipedia.org/wiki/Sudoku]
# 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.
def check_sudoku ( p ):
n = len(p) # extract size of grid
digit = 1
while digit <= n:
i = 0
while i < n:
row_count = 0
col_count = 0
j = 0
while j < n:
if p[i][j] == digit:
row_count = row_count + 1
if p[j][i] == digit:
col_count = col_count + 1
j = j + 1
if ((row_count ! = 1) or (col_count != 1)) :
return False
i = i + 1
digit = digit +1
return True