Row and Column Sums of a Grid

Medium ⏱ 12 min 64% acceptance ★★★★★ 4.9
Write row_and_column_sums(grid) for a rectangular 2D list grid (all rows the same length). Return a tuple (row_sums, col_sums) where row_sums[i] is the sum of row i and col_sums[j] is the sum of column j.

Examples

Example 1
Input
grid = [[1, 2, 3], [4, 5, 6]]
Output
([6, 15], [5, 7, 9])
Explanation

Row sums: 1+2+3=6, 4+5+6=15. Column sums: 1+4=5, 2+5=7, 3+6=9.

Constraints

  • 1 <= rows, cols <= 500

Topics

ListsMatrixNested Lists

Companies

AmazonUber

Hints

Hint 1

row_sums is just sum(row) for each row.

Hint 2

For col_sums, zip(*grid) transposes the grid so each tuple is a column you can sum.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.