Skip to content

Instantly share code, notes, and snippets.

@Eitol
Created September 7, 2017 03:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eitol/677f9d2c6455bdbe6905ecac33653a8d to your computer and use it in GitHub Desktop.
Save Eitol/677f9d2c6455bdbe6905ecac33653a8d to your computer and use it in GitHub Desktop.
HackerRank "Forming a Magic Square" python solution
import sys
# Solve https://www.hackerrank.com/challenges/magic-square-forming/problem
matrix_list = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]],
[[6, 1, 8], [7, 5, 3], [2, 9, 4]],
[[4, 9, 2], [3, 5, 7], [8, 1, 6]],
[[2, 9, 4], [7, 5, 3], [6, 1, 8]],
[[8, 3, 4], [1, 5, 9], [6, 7, 2]],
[[4, 3, 8], [9, 5, 1], [2, 7, 6]],
[[6, 7, 2], [1, 5, 9], [8, 3, 4]],
[[2, 7, 6], [9, 5, 1], [4, 3, 8]]]
def get_min_cost(mat: list) -> int:
cost_list = [sys.maxsize] * len(matrix_list)
for ref_mat in matrix_list:
cost = 0
for x in range(0, len(mat)):
for y in range(0, len(mat)):
if mat[x][y] != ref_mat[x][y]:
cost += abs(mat[x][y] - ref_mat[x][y])
cost_list.append(cost)
return min(cost_list)
# s = [[4, 8, 2], [4, 5, 7], [6, 1, 6]]
# s = [[4, 4, 7], [3, 1, 5], [1, 7, 9]]
# s = [[7, 2, 9], [6, 6, 2], [5, 1, 2]]
s = []
for s_i in range(3):
s_t = [int(s_temp) for s_temp in input().strip().split(' ')]
s.append(s_t)
print(get_min_cost(s))
@ashraftumwesigye
Copy link

can i get a little explanation

@ModarYaghi
Copy link

ModarYaghi commented Oct 18, 2020

Good try.
But you can do jus:

`

def forming_magic_square(s):
    s = sum(s, [])  # flaten s

    #  All possible magic squares of 3x3 order
    magic_squares = [
        [8, 1, 6, 3, 5, 7, 4, 9, 2],
        [6, 1, 8, 7, 5, 3, 2, 9, 4],
        [4, 9, 2, 3, 5, 7, 8, 1, 6],
        [2, 9, 4, 7, 5, 3, 6, 1, 8],
        [8, 3, 4, 1, 5, 9, 6, 7, 2],
        [4, 3, 8, 9, 5, 1, 2, 7, 6],
        [6, 7, 2, 1, 5, 9, 8, 3, 4],
        [2, 7, 6, 9, 5, 1, 4, 3, 8],
    ]

    costs = []  # this variable will contain all possible costs

    for magic_square in magic_squares:
        costs.append(sum([abs(magic_square[i] - s[i]) for i in range(9)]))

    return min(costs) 

`

@modityaGupta
Copy link

@ashraftumwesigye There are only 8 possible magic squares in 3 dimensions, so the solution is to compare with each one and find the difference, subsequently finding one with least cost.

@arunkmind
Copy link

import sys

matrix_list = [[[8, 1, 6], [3, 5, 7], [4, 9, 2]],
[[6, 1, 8], [7, 5, 3], [2, 9, 4]],
[[4, 9, 2], [3, 5, 7], [8, 1, 6]],
[[2, 9, 4], [7, 5, 3], [6, 1, 8]],
[[8, 3, 4], [1, 5, 9], [6, 7, 2]],
[[4, 3, 8], [9, 5, 1], [2, 7, 6]],
[[6, 7, 2], [1, 5, 9], [8, 3, 4]],
[[2, 7, 6], [9, 5, 1], [4, 3, 8]]]

def get_min_cost(mat: list) -> int:
cost_list = [sys.maxsize] * len(matrix_list)
for ref_mat in matrix_list:
cost = 0
for x in range(0, len(mat)):
for y in range(0, len(mat)):
if mat[x][y] != ref_mat[x][y]:
cost += abs(mat[x][y] - ref_mat[x][y])
cost_list.append(cost)
return min(cost_list)

s = [[4, 8, 2], [4, 5, 7], [6, 1, 6]]

s = [[4, 4, 7], [3, 1, 5], [1, 7, 9]]

s = [[7, 2, 9], [6, 6, 2], [5, 1, 2]]

s = []
for s_i in range(3):
s_t = [int(s_temp) for s_temp in input().strip().split(' ')]
s.append(s_t)
print(get_min_cost(s))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment