-
Notifications
You must be signed in to change notification settings - Fork 2
/
LoShu(4.3).py
152 lines (143 loc) · 4.3 KB
/
LoShu(4.3).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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-8.
The Lo Shu Magic Square has the following properties:
The grid contains the numbers 1 through 9 exactly.
The sum of each row, each column, and each diagonal all add up to the same number.
Write a function that accepts a two-dimensional list as an argument
and determines whether the list is a Lo Shu Magic Square. Test the function in a program."""
lst = [
[4,9,2],
[3,5,7],
[8,1,6]
]
flag_h = True
flag_v = True
flag_d = True
def magic_square(lis):
sumline = 15
flag_h = hori(lis)
flag_v = vert(lis)
flag_d = dia(lis)
#print (flag_h)
#print (flag_v)
#print (flag_d)
if flag_h == True and flag_v == True and flag_d == True:
print ('Congratz! Your square is a magic square!!! :D ')
else:
print ('Sorry, not a magic square... ')
def hori(ls):
for i in ls:
current1 = 0
for k in i:
current1+=k
#print(flag_checkh(current1))
if flag_checkh(current1) == False:
return False
return True
def vert(ls):
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 0:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 0:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 0:
current3+=value
#print (current3)
if flag_checkd(current3) == True:
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 1:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 1:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 1:
current3+=value
#print(current3)
if flag_checkd(current3) == True:
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 2:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 2:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 2:
current3+=value
#print (current3)
return True
else:
return False
else:
return False
#diagonal
def dia(ls):
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 0:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 1:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 2:
current3+=value
#print (current3)
if flag_checkd(current3) == True:
current3=0
for key,line in enumerate(ls):
if key == 0:
for each,value in enumerate(line):
if each == 2:
current3+=value
if key == 1:
for each,value in enumerate(line):
if each == 1:
current3+=value
if key == 2:
for each,value in enumerate(line):
if each == 0:
current3+=value
return True
#print(current3)
else:
return False
def flag_checkh(n):
if n == 15:
flag_h = True
else:
flag_h = False
return flag_h
def flag_checkv(n):
if n == 15:
flag_v = True
else:
flag_v = False
return flag_v
def flag_checkd(n):
if n == 15:
flag_d = True
else:
flag_d = False
return flag_d
magic_square(lst)