I am writing a python class that finds all possible magic squares with an integer sizeand a generator for the possible combinations. These combinations are tuples of length size**2and are split into a grid size& times; size. The code itself works fine, but reuse of the generator is required itertools.tee. In the example shown below, this causes the memory used by the thread to jump to 300 MB, since each value in the iterator is stored in a list.
from itertools import permutations, tee
class MagicSquare:
def __init__(self, size, combinations):
self.size = size
self.range = range(self.size)
self.combinations = combinations
def getGrid(self, entries):
return [ entries[self.size*i:self.size*(i+1)] for i in self.range ]
def checkGrid(self, grid):
check_sum = sum(grid[0])
if any( sum(row) != check_sum for row in grid ):
return False
if any( sum(row[col] for row in grid) != check_sum for col in self.range ):
return False
if sum(grid[diag][diag] for diag in self.range) != check_sum:
return False
if sum(grid[diag][self.size-diag-1] for diag in self.range) != check_sum:
return False
return True
def solutions(self):
combinations, self.combinations = tee(self.combinations)
for entries in combinations:
grid = self.getGrid(entries)
if self.checkGrid(grid):
yield grid
if __name__ == '__main__':
combs = permutations(range(20,30), 9)
ms = MagicSquare(3, combs)
for solution in ms.solutions():
for row in solution:
print row
print
, . -, , , , , . -, . , , , , checkGrid combinations.
, : ? , , , .
, Python 3.X copy.deepcopy itertools, .