-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScienceSquare.py
More file actions
40 lines (32 loc) · 2.13 KB
/
ScienceSquare.py
File metadata and controls
40 lines (32 loc) · 2.13 KB
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
import sys # Importing the great heavenly sys
matrix = [list(map(int, line.split())) for line in sys.stdin] # using map to use int on every single element of input, and then stuff it all in to an list
N = len(matrix) # getting the height of the matrix by just taking amount of rows
r0 = c0 = None # setting the coordinates of the 0 as nonexistent
for i in range(N): # for every single row
try:
r0, c0 = i, matrix[i].index(0) # try to set the index of an element 0
except: # if such element does not exist, and as such raises an error
pass # we just do not care and continue executing the code
if r0 is not None: # end the loop if we found the 0
break # anihilate the loop
for i in range(N): # get the first row
if i != r0: # that does not include the 0 element
target = sum(matrix[i]) # get it's sum
break # and exit the loop
missing = target - sum(matrix[r0]) # get the value for the 0
matrix[r0][c0] = missing # set the value in to the matrix
def magic(mat): # function for checking if the matrix is made by a magician and as such magical (we do not have to have a function but why not lol)
n = len(mat) # get the amount of rows of the matrix that was put in the argument
t = sum(mat[0]) # get the sum of the first row
for i in range(n): # iterate over each row
if sum(mat[i]) != t or sum(mat[j][i] for j in range(n)) != t: # and check if sum of the row doesn't match the target sum, or if sum of column doesn't match
return False # Destroy hopes and dreams
if sum(mat[i][i] for i in range(n)) != t or sum(mat[i][n-1-i] for i in range(n)) != t: # check diagonals, if they sum up to the target or not
return False # Destroy hopes and dreams #2
return True # Confirm the hopes, and be optimistic :)
if magic(matrix): # call the newely baked function
for r in matrix: # for each row in matrix
print(*r) # explode the row and print the remains of it
else: # else
print("Can't be magic") # and if the matrix ain't made by magician, then print it
# I hope it is not too many comments, my sleep schedule is already done for today, so might as well do more tasks