Форум сайта python.su
0
# 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. correct = [[1,2,3], [2,3,1], [3,1,2]] incorrect = [[1,2,3,4], [2,3,1,3], [3,1,2,3], [4,4,4,4]] def check_sudoku(p): n = len(p) 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 #print check_sudoku(incorrect) #>>> False print check_sudoku(correct) #>>> True
Отредактировано Kurtz (Март 20, 2016 21:57:05)
Офлайн
568
Берем список, кастуем его к множеству, сравниваем длину множества с длиной списка.
Офлайн
0
Что означает
p[i][j]
p[j][i]
Отредактировано Kurtz (Март 21, 2016 12:00:35)
Офлайн
0
Как форматированный набор из 33 строк кода, который в итоге выдает 4 строки:
>>> False True >>>
Офлайн