Created
March 13, 2020 19:31
-
-
Save wellington-tinho/d79f4b10227e40dd8e4668724d0dd23d to your computer and use it in GitHub Desktop.
Um quadrado mágico é um quadrado onde a soma das linhas, colunas e diagonais é a mesma. (ex: de quadrado onde a soma é igual a 15).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import permutations | |
def Avaliador(lista): | |
#linha 1 | |
result=0 | |
for i in subset[:3]: | |
result=result+i | |
if result!=15: | |
return False | |
#linha 2 | |
result=0 | |
for i in subset[3:6]: | |
result+=i | |
if result!=15: | |
return False | |
result=0 | |
#linha 3 | |
for i in subset[6:]: | |
result+=i | |
if result!=15: | |
return False | |
#coluna 1 | |
result=0 | |
result=subset[0]+subset[3]+subset[6] | |
if result!=15: | |
return False | |
#coluna 2 | |
result=0 | |
result=subset[1]+subset[4]+subset[7] | |
if result!=15: | |
return False | |
#coluna 3 | |
result=0 | |
result=subset[2]+subset[5]+subset[8] | |
if result!=15: | |
return False | |
#diagonal primaria | |
result=0 | |
result=subset[0]+subset[4]+subset[8] | |
if result!=15: | |
return False | |
#diagonal segunda | |
result=0 | |
result=subset[2]+subset[4]+subset[6] | |
if result!=15: | |
return False | |
return True | |
caracteres = [1, 2,3,4,5,6,7,8,9] | |
for subset in permutations(caracteres, 9): | |
if Avaliador(subset): | |
print(subset[:3],"\n",subset[3:6],"\n",subset[6:]) | |
print('\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment