Advertisement
CaelmBleidd

Untitled

Feb 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. def read_sudoku(file_name):
  2.     digits = [c for c in open(file_name).read() if c in '1234567890']
  3.     matrix = []
  4.     for i in range(9):
  5.         line = []
  6.         for j in range(9):
  7.             line.append(int(digits[j + 9 * i]))
  8.         matrix.append(line)
  9.     return matrix
  10.  
  11.  
  12. def get_row(matrix, row_number):
  13.     return matrix[row_number]
  14.  
  15.  
  16. def get_col(matrix, col_number):
  17.     col = []
  18.     for i in range(9):
  19.         col.append(matrix[i][col_number])
  20.     return col
  21.  
  22.  
  23. def get_block(matrix, position):
  24.     col = int(position[1] / 3) * 3
  25.     row = int(position[0] / 3) * 3
  26.     block = []
  27.     for i in range(3):
  28.         for j in range(3):
  29.             block.append(matrix[row + i][col + j])
  30.     return block
  31.  
  32.  
  33. def find_empty_position(matrix):
  34.     for i in range(9):
  35.         for j in range(9):
  36.             if matrix[i][j] == 0:
  37.                 return i, j
  38.     return -1, -1
  39.  
  40.  
  41. def find_possible_values(matrix, position):
  42.     row = get_row(matrix, position[0])
  43.     col = get_col(matrix, position[1])
  44.     block = get_block(matrix, position)
  45.     is_exist = []
  46.     for i in range(10):
  47.         is_exist.append(True)
  48.     for i in range(9):
  49.         if row[i] != 0:
  50.             is_exist[row[i]] = False
  51.         if col[i] != 0:
  52.             is_exist[col[i]] = False
  53.         if block[i] != 0:
  54.             is_exist[block[i]] = False
  55.     possible_values = []
  56.     for i in range(1, 10):
  57.         if is_exist[i]:
  58.             possible_values.append(i)
  59.     return possible_values
  60.  
  61.  
  62. def is_end(matrix):
  63.     position = find_empty_position(matrix)
  64.     if position[0] == -1:
  65.         return True
  66.     return False
  67.  
  68.  
  69. def solve_sudoku(matrix):
  70.     position = find_empty_position(matrix)
  71.     row = position[0]
  72.     col = position[1]
  73.     values = find_possible_values(matrix, position)
  74.     if len(values) == 0:
  75.         return matrix
  76.     for i in values:
  77.         matrix[row][col] = i
  78.         if is_end(matrix):
  79.             return matrix
  80.         matrix = solve_sudoku(matrix)
  81.         if is_end(matrix):
  82.             return matrix
  83.     matrix[row][col] = 0
  84.     return matrix
  85.  
  86.  
  87. def print_sudoku(matrix):
  88.     for i in range(9):
  89.         if i % 3 == 0 and i != 0:
  90.             print('\n')
  91.         for j in range(9):
  92.             if j % 3 == 0 and j != 0:
  93.                 print("    ", end="")
  94.             print(matrix[i][j], end=" ")
  95.         print()
  96.  
  97.  
  98. def check_solution(matrix):
  99.     return check_blocks(matrix) and check_cols(matrix) and check_rows(matrix)
  100.  
  101.  
  102. def check_blocks(matrix):
  103.     for i in range(0, 9, 3):
  104.         for j in range(0, 9, 3):
  105.             block = get_block(matrix, (i, j))
  106.             if not common_check(block):
  107.                 return False
  108.     return True
  109.  
  110.  
  111. def check_rows(matrix):
  112.     for i in range(9):
  113.         row = get_row(matrix, i)
  114.         if not common_check(row):
  115.             return False
  116.     return True
  117.  
  118.  
  119. def check_cols(matrix):
  120.     for i in range(9):
  121.         col = get_col(matrix, i)
  122.         if not common_check(col):
  123.             return False
  124.     return True
  125.  
  126.  
  127. def common_check(values):
  128.     result = []
  129.     for k in range(10):
  130.         result.append(0)
  131.     for k in values:
  132.         result[k] += 1
  133.     for k in range(1, 10):
  134.         if result[k] != 1:
  135.             return False
  136.     return True
  137.  
  138.  
  139. print("Enter the name of the file with sudoku: ", end="")
  140. fileName = input()
  141. sudoku = read_sudoku(fileName)
  142. sudoku = solve_sudoku(sudoku)
  143. print_sudoku(sudoku)
  144. print("Do you want check solution? Y/N")
  145. answer = input().lower()
  146. if answer == "yes" or answer == 'y':
  147.     print(check_solution(sudoku))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement