You are viewing a single comment's thread. Return to all comments →
Kinda clunky and verbose but uses only 1 for loop(2 if we count "min"), no "if elses" and it's easy to understand:
def formingMagicSquare(s): msp = { 0:[8,1,6,3,5,7,4,9,2], 1:[6,1,8,7,5,3,2,9,4], 2:[4,9,2,3,5,7,8,1,6], 3:[2,9,4,7,5,3,6,1,8], 4:[8,3,4,1,5,9,6,7,2], 5:[4,3,8,9,5,1,2,7,6], 6:[6,7,2,1,5,9,8,3,4], 7:[2,7,6,9,5,1,4,3,8], } box = [*s[0], *s[1], *s[2]] result = [0, 0, 0, 0, 0, 0, 0, 0] for x in range(9): result[0] += abs(msp[0][x] - box[x]) result[1] += abs(msp[1][x] - box[x]) result[2] += abs(msp[2][x] - box[x]) result[3] += abs(msp[3][x] - box[x]) result[4] += abs(msp[4][x] - box[x]) result[5] += abs(msp[5][x] - box[x]) result[6] += abs(msp[6][x] - box[x]) result[7] += abs(msp[7][x] - box[x]) return min(result)
Forming a Magic Square
You are viewing a single comment's thread. Return to all comments →
Kinda clunky and verbose but uses only 1 for loop(2 if we count "min"), no "if elses" and it's easy to understand: