May-09-2019, 06:29 AM
This is a new code for magic square with same problem. I'm asking how filter a single magic square, after the script has been processed.
The output is:
2,7,6
9,5,1
4,3,8
----
2,9,4
7,5,3
6,1,8
----
3,8,7
10,6,2
5,4,9
----
3,10,5
8,6,4
7,2,9
----
4,3,8
9,5,1
2,7,6
----
4,9,2
3,5,7
8,1,6
----
5,4,9
10,6,2
3,8,7
----
5,10,3
4,6,8
9,2,7
----
6,1,8
7,5,3
2,9,4
----
6,7,2
1,5,9
8,3,4
----
7,2,9
8,6,4
3,10,5
----
7,8,3
2,6,10
9,4,5
----
8,1,6
3,5,7
4,9,2
----
8,3,4
1,5,9
6,7,2
----
9,2,7
4,6,8
5,10,3
----
9,4,5
2,6,10
7,8,3
----
I would output without repetition, like this:
2,7,6
9,5,1
4,3,8
-----
9,4,5
2,6,10
7,8,3
The output is:
2,7,6
9,5,1
4,3,8
----
2,9,4
7,5,3
6,1,8
----
3,8,7
10,6,2
5,4,9
----
3,10,5
8,6,4
7,2,9
----
4,3,8
9,5,1
2,7,6
----
4,9,2
3,5,7
8,1,6
----
5,4,9
10,6,2
3,8,7
----
5,10,3
4,6,8
9,2,7
----
6,1,8
7,5,3
2,9,4
----
6,7,2
1,5,9
8,3,4
----
7,2,9
8,6,4
3,10,5
----
7,8,3
2,6,10
9,4,5
----
8,1,6
3,5,7
4,9,2
----
8,3,4
1,5,9
6,7,2
----
9,2,7
4,6,8
5,10,3
----
9,4,5
2,6,10
7,8,3
----
I would output without repetition, like this:
2,7,6
9,5,1
4,3,8
-----
9,4,5
2,6,10
7,8,3
class MagicSquare : def __init__(self, coef) : self.mat = [ [ coef[i+j*3] for i in range(3) ] for j in range(3) ] def __str__(self) : return "\n".join ( [ ",".join( [ str(n) for n in row ] ) for row in self.mat ] ) def __add__ (self, Square) : coef = [] for i in range(3) : for j in range(3) : coef.append ( self.mat[i][j] + Square.mat[i][j]) return MagicSquare(coef) def Sum_H_V_D(self): tout = [ sum ( lines ) for lines in self.mat ] + \ [ sum ( self.mat[i][j] for i in range(3) ) for j in range(3) ] + \ [ sum ( self.mat[i][i] for i in range(3) ) ] + \ [ sum ( self.mat[2-i][i] for i in range(3) ) ] return tout return sorted(tout) def Coeff_unique(self): d = { } for lines in self.mat : for c in lines : d [c] = d.get(c,0) +1 return len(d) == 9 def is_magic(self): unique = self.Coeff_unique() if not unique : return False additions = self.Sum_H_V_D() return min(additions) == max(additions) return sorted(additions) def all_magic_squares(): res=[] lst=([1,2,3,4,5,6,7,8,9,10]) print() for a1 in lst: for a2 in lst: for a3 in lst: for b1 in lst: additions = a1 + a2 + a3 c1 = additions - a1 - b1 b2 = additions - a3 - c1 b3 = additions - b1 - b2 c2 = additions - a2 - b2 c3 = additions - c1 - c2 M = MagicSquare([a1,a2,a3, b1,b2,b3, c1,c2,c3]) if M.is_magic() and 0 < b2 < 91 and 0 < b3 < 91 and 0 < c1 < 91 and 0 < c2 < 91 and 0 < c3 < 91 and b1 in lst and b2 in lst and b3 in lst and c2 in lst and c3 in lst: res.append (M) print(M) print ("----") return res res = all_magic_squares()