Skip to the code
cf.
+10 XP

#75 Transpose a Data Table

easy

A data grid component has a "swap rows and columns" button. The table is table, an array of rows of equal length.

Write transpose(table) that returns a new table where row c holds what was column c: the cell at row r, column c moves to row c, column r.

Example 1

Input: table = [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]

The 2 by 3 table becomes 3 by 2: its first column 1, 4 is now the first row.

Example 2

Input: table = [[7]]
Output: [[7]]

A single cell stays put.

Constraints

  • 1 <= rows, cols <= 1000
  • Cells are numbers or strings.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).