-
Notifications
You must be signed in to change notification settings - Fork 0
/
magicSquare2.py
69 lines (58 loc) · 1.75 KB
/
magicSquare2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Python3 program to check whether a given
# matrix is magic matrix or not
# Returns true if mat[][] is magic
# square, else returns false.
def isMagicSquare(mat):
n = len(mat)
# sumd1 and sumd2 are the sum of the two diagonals
sumd1 = 0
sumd2 = 0
for i in range(n):
# (i, i) is the diagonal from top-left -> bottom-right
# (i, n - i - 1) is the diagonal from top-right -> bottom-left
sumd1 += mat[i][i]
sumd2 += mat[i][n-i-1]
# if the two diagonal sums are unequal then it is not a magic square
if not (sumd1 == sumd2):
return False
for i in range(n):
#sumr is rowsum and sumc is colsum
sumr = 0
sumc = 0
for j in range(n):
sumr += mat[i][j]
sumc += mat[j][i]
if not (sumr == sumc == sumd1):
return False
# if all the conditions are satisfied then it is a magic square
return True
if (isMagicSquare()):
print("Magic Square")
else:
print("Not a magic Square")
def printTable(table):
for i in range(4):
for j in range(4):
print("%8d" % table[i][j], end="")
print()
def makeTable(string):
table = [
[string[0], string[1], string[2], string[3]],
[string[4], string[5], string[6], string[7]],
[string[8], string[9], string[10], string[11]],
[string[12], string[13], string[14], string[15]]
]
printTable(table)
def stringCreation(string):
val = 0
i = 0
num = [0] * 16
for k in range(16):
number = string[i] + string[i+1]
val = int(number)
num[k] = val
i = i+3
return num
string = input("Enter string: ")
makeTable(stringCreation(string))
# 01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16