Advertisement
Guest User

Untitled

a guest
May 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import sys;
  2.  
  3. def rowSum(l, rIndex):
  4. return sum(l[rIndex])
  5.  
  6. def colSum(l, cIndex):
  7. return sum([l[_][cIndex] for _ in range(len(l))])
  8.  
  9. def zeroPos(l, len):
  10. return [[r, c] for r in range(len) for c in range(len) if l[r][c] == 0]
  11.  
  12. N = int(input())
  13. SUM = int(N * (N ** 2 + 1) / 2)
  14. magic_matrix = [list(map(int, l.strip().split())) for l in sys.stdin.readlines() if l != '\n']
  15. zero_pos = zeroPos(magic_matrix, N)
  16. is_same_row = zero_pos[0][0] == zero_pos[1][0]
  17.  
  18. for [r, c] in zero_pos:
  19. if is_same_row:
  20. magic_matrix[r][c] = SUM - colSum(magic_matrix, c)
  21. else:
  22. magic_matrix[r][c] = SUM - rowSum(magic_matrix, r)
  23.  
  24. for i in range(N):
  25. print(" ".join(list(map(str,magic_matrix[i]))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement