Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys;
- def rowSum(l, rIndex):
- return sum(l[rIndex])
- def colSum(l, cIndex):
- return sum([l[_][cIndex] for _ in range(len(l))])
- def zeroPos(l, len):
- return [[r, c] for r in range(len) for c in range(len) if l[r][c] == 0]
- N = int(input())
- SUM = int(N * (N ** 2 + 1) / 2)
- magic_matrix = [list(map(int, l.strip().split())) for l in sys.stdin.readlines() if l != '\n']
- zero_pos = zeroPos(magic_matrix, N)
- is_same_row = zero_pos[0][0] == zero_pos[1][0]
- for [r, c] in zero_pos:
- if is_same_row:
- magic_matrix[r][c] = SUM - colSum(magic_matrix, c)
- else:
- magic_matrix[r][c] = SUM - rowSum(magic_matrix, r)
- for i in range(N):
- print(" ".join(list(map(str,magic_matrix[i]))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement