Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3d9b4cd

Browse files
Add files via upload
1 parent f9a08c6 commit 3d9b4cd

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

‎Forming a Magic Square.py

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/bin/python3
2+
3+
import math
4+
import os
5+
import random
6+
import re
7+
import sys
8+
9+
# Complete the formingMagicSquare function below.
10+
def formingMagicSquare(s):
11+
12+
13+
data = [
14+
[[8, 1, 6], [3, 5, 7], [4, 9, 2]],
15+
[[6, 1, 8], [7, 5, 3], [2, 9, 4]],
16+
[[4, 9, 2], [3, 5, 7], [8, 1, 6]],
17+
[[2, 9, 4], [7, 5, 3], [6, 1, 8]],
18+
[[8, 3, 4], [1, 5, 9], [6, 7, 2]],
19+
[[4, 3, 8], [9, 5, 1], [2, 7, 6]],
20+
[[6, 7, 2], [1, 5, 9], [8, 3, 4]],
21+
[[2, 7, 6], [9, 5, 1], [4, 3, 8]],
22+
]
23+
24+
25+
t = []
26+
for i in data:
27+
res = 0
28+
for j, k in zip(i,s):
29+
for x,y in zip(j,k):
30+
res += max([x,y]) - min([x,y])
31+
t.append(res)
32+
return min(t)
33+
34+
if __name__ == '__main__':
35+
fptr = open(os.environ['OUTPUT_PATH'], 'w')
36+
37+
s = []
38+
39+
for _ in range(3):
40+
s.append(list(map(int, input().rstrip().split())))
41+
42+
result = formingMagicSquare(s)
43+
44+
fptr.write(str(result) + '\n')
45+
46+
fptr.close()
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+
# We define a magic square to be an matrix of distinct positive integers from to where the sum of any row, column, or diagonal of length is always equal to the same number: the magic constant.
91+
92+
# You will be given a matrix of integers in the inclusive range . We can convert any digit to any other digit in the range at cost of . Given , convert it into a magic square at minimal cost. Print this cost on a new line.
93+
94+
# Note: The resulting magic square must contain distinct integers in the inclusive range .
95+
96+
# For example, we start with the following matrix :
97+
98+
# 5 3 4
99+
# 1 5 8
100+
# 6 4 2
101+
# We can convert it to the following magic square:
102+
103+
# 8 3 4
104+
# 1 5 9
105+
# 6 7 2
106+
# This took three replacements at a cost of .
107+
108+
# Function Description
109+
110+
# Complete the formingMagicSquare function in the editor below. It should return an integer that represents the minimal total cost of converting the input square to a magic square.
111+
112+
# formingMagicSquare has the following parameter(s):
113+
114+
# s: a array of integers
115+
# Input Format
116+
117+
# Each of the lines contains three space-separated integers of row .
118+
119+
# Constraints
120+
121+
# Output Format
122+
123+
# Print an integer denoting the minimum cost of turning matrix into a magic square.
124+
125+
# Sample Input 0
126+
127+
# 4 9 2
128+
# 3 5 7
129+
# 8 1 5
130+
# Sample Output 0
131+
132+
# 1
133+
# Explanation 0
134+
135+
# If we change the bottom right value, , from to at a cost of , becomes a magic square at the minimum possible cost.
136+
137+
# Sample Input 1
138+
139+
# 4 8 2
140+
# 4 5 7
141+
# 6 1 6
142+
# Sample Output 1
143+
144+
# 4
145+
# Explanation 1
146+
147+
# Using 0-based indexing, if we make
148+
149+
# -> at a cost of
150+
# -> at a cost of
151+
# -> at a cost of ,
152+
# then the total cost will be .

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /