All problems
Count Magic Squares
Problem
Count the 3 by 3 contiguous subgrids of grid that are magic squares: they contain each digit from 1 to 9 exactly once and every row, column and diagonal sums to 15.
Check every possible top-left corner of a 3 by 3 window. Return 0 for grids smaller than 3 by 3.
Hint
A 3 by 3 magic square must be a permutation of 1 through 9 and every line sums to 15.
Test cases
case 1
in: [[[4,3,8,4],[9,5,1,9],[2,7,6,2]]]
expected: 1
case 2
in: [[[8]]]
expected: 0
case 3
in: [[]]
expected: 0
case 4
in: [[[5,5,5],[5,5,5],[5,5,5]]]
expected: 0
case 5
in: [[[4,3,8],[9,5,1],[2,7,6]]]
expected: 1
Starter codePython
def count_magic_squares(grid):
# Your code here
pass5 test cases · run your code against them in the editor