Created
September 7, 2017 03:28
-
-
Save Eitol/677f9d2c6455bdbe6905ecac33653a8d to your computer and use it in GitHub Desktop.
HackerRank "Forming a Magic Square" python solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
def formingMagicSquare(s):
s = sum(s, []) # Flatten the 2D list 's' into a 1D list
magic = [
[8, 1, 6, 3, 5, 7, 4, 9, 2], # These are all possible 3x3 magic squares
[6, 1, 8, 7, 5, 3, 2, 9, 4], # Flattened into 1D lists
[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],
]
mincost = sys.maxsize # Initialize mincost to a large value
for mag in magic:
diff = 0 # This will store the difference for the current magic square
for i, j in zip(s, mag): # Iterate through elements of both 's' and 'mag'
diff += abs(i - j) # Add the absolute difference between corresponding elements
mincost = min(mincost, diff) # Update the mincost if the current cost is lower
return mincost
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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))