Upload
phamhanh
View
229
Download
0
Embed Size (px)
Citation preview
5
3! 5! 1! 0! 9!
12! 16! 12! 34! 2!
0! 0! 23! 5! 11!
7! 51! 8! 0! 33!
0 1 2 3 4!
0!
1!
2!
3!
rows
columns
6
1st row 2nd row
n-th row
1st value
2nd value
m-th value
7 7
def matrix(numrows, numcolumns) :! # builds a matrix with the specified number of rows and columns! # all matrix entries are initialized to 0! mat = []! for i in range(0, numrows) :! mat.append([])! for j in range(0, numcolumns) :! mat[i].append(0)! return mat !
>>> my_matrix=matrix(2,3)!>>> print my_matrix![[0, 0, 0], [0, 0, 0]]!
8
Working with matrices
a more user-friendly print function
8
def print_mat(matrix) :! for row in matrix :! print row!
>>> my_matrix=matrix(2,3)!>>> print_mat(my_matrix)![0, 0, 0]![0, 0, 0]!
>>> my_matrix[0][1]=3!>>> print_mat(my_matrix)![0, 3, 0]![0, 0, 0]!>>> my_matrix[1][2]=1+4!>>> print_mat(my_matrix)![0, 3, 0]![0, 0, 5]!
assigning values to matrix entries we use the m[i][j] notation
9
Working with matrices
building a matrix and reading interactively its entries from input
9
def read_matrix(numrows, numcolumns) :! # @param numrows: int value specifieng the number of rows! # @param numcolumns: int value specifieng the number of columns! # @return: the matrix read from input! #! # builds a matrix with the specified number of rows and columns! # and initializes its entries with values taken from input! # one row at a time, specified as a list of numcolumns values! m = matrix(numrows, numcolumns)! for i in range(0, numrows) :! m[i] = input(“write a row as a list of values (enclosed in square brackets): “)! return m!
10
Working with matrices
matrix algebra: summing two matrices
10
def add(m1, m2) :! # it requires that m1 and m2 are matrices with same number of rows ! # and same number of columns! # NOTE: len(mx) is the number of rows of mx! # NOTE: len(mx[0]) is the number of columns of mx! if len(m1)==len(m2) and len(m1[0])==len(m2[0]) :! s = matrix(len(m1),len(m1[0]))! for i in range(0,len(m1)) :! for j in range(0,len(m1[0])):! s[i][j] = m1[i][j]+m2[i][j]! else :! s = [] ! print "matrix dimensions do not match"! return s !
11
12
def sudoku_check(sudoku_mat) :!# @param sudoku_mat: a square matrix (usually 9x9) that contains the proposed sudoku solution!# @return: none!# the called xxx_check() functions return a negative value when the check is OK!# otherwise a non negative integer indicating the error location! ##### row check! result = row_check(sudoku_mat)! if result >= 0 :! print "error at row "+str(result) #str(n) converts n to a string! else : ##### column check! result = col_check(sudoku_mat)! if result >= 0 :! print "error at column "+str(result) #str(n) converts n to a string! else : ##### block check! result = block_check(sudoku_mat)! if result >= 0 :! print "error at block ” + str(result) #str(n) converts n to a string! #### final response! if result >= 0 :! print "your solution is not correct; try again"! else :! print "your solution is correct; you are a good sudoku player" !
13
def vect_check(vect) :!# @param vect: a uni-dimensional vector!# @return: TRUE if vect contains all integer values from 1 to len(vect), in any order; FALSE otherwise! bool_vect = []! # build a vector with same number of entries as vect, all initialized to false, ! for i in range(0,len(vect)) :! bool_vect = bool_vect+[false]! ok = true! # start setting bool_vect entries to true, according to values contained in vect! # if an entry of bool_vect results already true, then a value in vect is repeated at least twice (error)! i = 0! ok = true! while ok and i<len(vect) :! if bool_vect[vect[i]-1] == false :! bool_vect[vect[i]-1] = true! else :! ok = false! i = i+1! return ok!
14
def row_check(sudoku_mat) :!# @param sudoku_mat: a square matrix (usually 9x9) that contains the proposed sudoku solution !# @return: a negative value if sudoku_mat is correct by row, !# otherwise an index i>=0 indicating the first found incorrect row! row_num = len(sudoku_mat)! for i in range(0, row_num) :! if not vect_check(sudoku_mat[i]) : # row i is not correct! return i # stop execution and return row index! return -11 # it could be any negative value!
15
16
def matrix_col_to_vect(mat, j) :!# @param mat: a bi-dimensional matrix provided as input !# @param j: a column index for mat !# @return: a uni-dimensional vector (represented as a list) built with all entries of the j-th column! v = []! for i in range(0, len(mat)): # len(mat) is the number of rows of mat! v = v + [mat[i][j]]! return v!
def col_check(sudoku_mat) :!# @param sudoku_mat: a square matrix (usually 9x9) that contains the proposed sudoku solution !# @return: a negative value if sudoku_mat is correct by column, !# otherwise an index j>=0 indicating the first found incorrect column! col_num =l en(sudoku_mat[0])! for j in range(0,col_num) :! col = matrix_col_to_vect(sudoku_mat,j)! if not vect_check(col) : # column j is not correct! return j # stop execution and return column index! return -11 # it could be any negative value!
17
def block_check(sudoku_mat) :!# @param sudoku_mat: a square matrix (usually 9x9) that contains the proposed sudoku solution !# @return: a negative value if sudoku_mat is correct by block, !# otherwise an index i>=0 indicating the first found incorrect block!# blocks are numbered (starting from 1) from left to right, from top to bottom! row_num = len(sudoku_mat)! col_num = len(sudoku_mat[0])! block_num = 1! for i in range(0,row_num,3) : # i takes value in [0, 3, 6]! for j in range(0, col_num,3) : # j takes value in [0, 3, 6] ! block = []! for h in range(i,i+3):! for k in range(j,j+3):! block = block+[sudoku_mat[h][k]]! if not vect_check(block) : # block block_num is not correct! return block_num # stop execution and return block number! block_num = block_num+1! return -11 # it could be any negative value!
18
def random_sudoku():!# @return: a 9x9 matrix randomly filled with values from 1 and 9!# Note: it is quite unlikely that you will get a correct sudoku solution :-)! from random import *! mat = matrix(9,9)! for i in range(0,9):! for j in range(0,9):! mat[i][j] = 1 + int(9*random())! return mat!
>>> m = random_sudoku()!>>> sudoku_check(m)!error at row 0!your solution is not correct; try again!