Skip to content

Instantly share code, notes, and snippets.

@zokis
Forked from pvleite/QMagico.py
Last active August 29, 2015 14:03
Show Gist options
  • Save zokis/333b27da80695116aa3b to your computer and use it in GitHub Desktop.
Save zokis/333b27da80695116aa3b to your computer and use it in GitHub Desktop.
def valida_q_magico(q_magico):
if len(q_magico) == 0 or len(q_magico) != len(q_magico[0]):
return False
linhas_ok = not sum(
1 if sum(linha) != sum(q_magico[0]) else 0
for linha in q_magico
)
colunas_ok = not sum(
sum(
linha[i] for linha in q_magico
) != sum(q_magico[0])
for i in range(len(q_magico[0]))
)
diagonais_ok = True
for i in range(len(q_magico[0])):
if sum(
linha[i] for linha in q_magico
) != sum(q_magico[0]):
diagonais_ok = False
i += 1
return linhas_ok and colunas_ok and diagonais_ok
q_magico = [ [2, 7, 6], [9, 5, 1], [4, 3, 8] ]
print q_magico, valida_q_magico(q_magico)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment