Skip to content

Instantly share code, notes, and snippets.

@BharathKumarS
Last active February 16, 2020 22:35
Show Gist options
  • Save BharathKumarS/d0072a70dcf59a79f0fc217f44e6711f to your computer and use it in GitHub Desktop.
Save BharathKumarS/d0072a70dcf59a79f0fc217f44e6711f to your computer and use it in GitHub Desktop.
Partial solution to build a Magic matrix of length 3
import os
import random
import re
import sys
import numpy as np
def VirginSquare(sq):
host = np.zeros(shape=(3,3))
missing = list()
index = list()
r = 0
d = 1
c = 2
if s[r][r] % 2 != 0:
s[r][r] = 0
elif s[r][c] %2 != 0:
s[r][c] = 0
elif s[c][r] %2 != 0:
s[c][r] = 0
elif s[c][c] %2 != 0:
s[c][c] = 0
if s[r][d] % 2 == 0:
s[r][d] = 0
elif s[d][r] % 2 == 0:
s[d][r] = 0
elif s[d][d] != 5:
s[d][d] = 5
elif s[d][c] % 2 == 0:
s[d][c] = 0
elif s[c][d] % 2 == 0:
s[c][d] = 0
for i in range(len(s)):
for j in range(len(s)):
if any(s[i][j] in x for x in host):
host[i][j] = 0
index.append(i)
index.append(j)
else:
host[i][j] = s[i][j]
for i in range(1,10):
if not any(i in x for x in host):
if i%2 != 0:
missing.append(i)
else:
missing.append(i)
if len(index) == 2:
host[index[0]][index[1]] = missing[0]
print(host)
print(missing)
print(index)
if __name__ == '__main__':
sq = []
for _ in range(3):
s.append(list(map(int, input().rstrip().split())))
VirginSquare(sq)
@BharathKumarS
Copy link
Author

Sample input and put put
image

We all know the magic matrix has a pattern for integer placement within the matrix. Integers in the center row and center column should all be an odd number, the left top, right top, left bottom and right bottom integers should be even numbers in a 3 X 3 matrix. My approach to design a solution for the Magic Matrix problem was to rewrite the given matrix by replacing any integer with zero if they are violating the integer placement rule and if the integers are repeating. The above piece of code is based on the facts that the integer at [1][1] should be five, the sum of the matrix has to be 45 and it should have integers ranging from 1-9 inclusive. My idea was to create a list for missing integers from the rewritten matrix, replace all the zeros with the from the list of missing integers. This solution works partially but when it comes to swapping the integer placement there is no ideal solution. At least not now.

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