Skip to content

Instantly share code, notes, and snippets.

@rayansostenes
Created June 13, 2017 20:22
Show Gist options
  • Save rayansostenes/2858fee21ef710a9d840f06ba66318b4 to your computer and use it in GitHub Desktop.
Save rayansostenes/2858fee21ef710a9d840f06ba66318b4 to your computer and use it in GitHub Desktop.
# rodrigo.deodoro@esharesinc.com
def validate_matrix(matrix):
# Valida linhas
if not all(list_int(i) for i in matrix):
return False
# Valida colunas
columns = []
for c in range(9):
columns.append([])
for l in range(9):
columns[c].append(matrix[l][c])
if not all(list_int(c) for c in columns):
return False
# Valida grupos
groups = []
for g in range(9):
r = (g // 3) * 3
c = g * 3 % 9
groups.append([])
for line in range(r, r+3):
for col in range(c, c+3):
groups[g].append(matrix[line][col])
if not all(list_int(g) for g in groups):
return False
return True
def valid_choice(number, chosen):
if number > 9 or number < 1:
return False
return number not in chosen
def list_int(numbers):
if not numbers:
return False
return sorted(numbers) == list(range(1, 10))
def main():
# print(list_int([]))
# print(list_int(None))
# print(list_int([1, 2]))
# print(list_int([1, 2, 3, 4, 5, 9, 7, 6, 8]))
# print(list_int([-1]))
# print(list_int([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]))
# print(list_int([1, 1, 1, 1, 1, 1, 1, 1, 1]))
# print(list_int([1, 2, 3, 4, 5, 6, 8, 9, 9]))
# print(valid_choice(3, [3, 6]))
# print(valid_choice(10, [3, 6]))
# print(valid_choice(5, [3, 6]))
matrix = [
[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]
]
print(validate_matrix(matrix))
if __name__ == '__main__':
import sys
sys.exit(int(main() or 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment