Matrix Row and Column Sums
Completed
Read r and c, then r lines of c integers. Print the sum of each row on one line, then the sum of each column on the next line.
Input
Line 1: r c. Then r rows.
Output
Two lines.
Example
Input
2 3 1 2 3 4 5 6
Output
6 15 5 7 9
Editor
Python
Hints
A 2D list is a list of lists: grid[row][col].
A column sum adds grid[i][j] for every row i.
Solution explanation
Reading a grid with a comprehension keeps the code short. zip(*grid) is another way to get columns.
Show solution
r, c = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(r)]
print(*[sum(row) for row in grid])
print(*[sum(grid[i][j] for i in range(r)) for j in range(c)])