-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicSquare.py
65 lines (55 loc) · 1.73 KB
/
magicSquare.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
def formingMagicSquare(s):
"""
##########Pseudocode##########
empty_dict = {}
notFound = []
excess = []
sqr = math.sqrt(len(s))
for i in range(sqr):
for j in range(sqr):
counter = s[i].count(s[i][j])
if s[i][j] in empty_dict:
empty_dict[s[i][j]] += counter
else:
empty_dict[s[i][j]]
for i in range(1, len(s)):
if i not in empty_dict:
notFound.append(i)
for i in empty_dict:
if empty_dict[i] > 1:
for j in range(empty_dict[i] - 1):
excess.append(empty_dict[i])
sum = 0
for i in range(len(excess)):
sub = math.abs(notFound[i] - excess[i])
sum += sub
return sum
"""
empty_dict = {}
notFound = []
excess = []
lenght = 1
for i in range(len(s)):
for j in range(len(s[i])):
lenght += 1
counter = s[i].count(s[i][j])
print(s[i][j], counter)
if s[i][j] in empty_dict and s[i].count(s[i][j]) == 1:
empty_dict[s[i][j]] += counter
else:
empty_dict[s[i][j]] = counter
for i in range(1, lenght):
if i not in empty_dict:
notFound.append(i)
for i in empty_dict:
if empty_dict[i] > 1:
for j in range(empty_dict[i] - 1):
excess.append(i)
sum = 0
print(empty_dict)
print(notFound)
print(excess)
for i in range(len(excess)):
sub = abs(notFound[i] - excess[i])
sum += sub
return sum