def generate(size): matrix = {} matrix["height"] = size matrix["width"] = size for row in range(size): for col in range(size): matrix[row, col] = 0 return matrix def show(matrix): for row in range(matrix["height"]): for col in range(matrix["width"]): print str(matrix[row, col]).rjust(4), print def matrixString(matrix): result = "" for row in range(matrix["height"]): for col in range(matrix["width"]): result = result + str(matrix[row, col]).rjust(4) result = result + "\n" return result def magic(matrix): size = matrix["height"] row = size-1 col = size/2 for k in range(1, size*size+1): matrix[row, col] = k newrow = row + 1 newcol = col + 1 if newrow >= size: newrow = 0 if newcol >= size: newcol = 0 if (matrix[newrow, newcol] != 0) or (row == size-1 and col == size-1): row = row - 1 # col = col else: row = newrow col = newcol show(matrix) print "-" * 20 def checkRows(matrix): for row in range(matrix["height"]): sum = 0 for col in range(matrix["width"]): sum += matrix[row, col] print sum