livepython.net

227. Python Program to Transpose a Matrix

HardPhase 3 · Challenge

This takes a few steps with tools you know: a list of lines, split(), join() and two for loops. Draw a small table on paper first.

Overview

A matrix is a table of numbers in rows and columns. Turning its rows into columns is called transposing it. Spreadsheets can do this, and it is a common interview question.

Problem Statement

Chloé keeps a table of points. Each row is one player, and each column is one game. The coach wants each game as a row instead.

Read the table. Print it again, but turned: the first column becomes the first line, the second column becomes the second line, and so on. Inside each line, the numbers keep their order from top to bottom.

Examples

Example 1. Input:

2 3
1 2 3
4 5 6

gives:

1 4
2 5
3 6

Why: the table has 2 rows and 3 columns. The first column is 1 and 4, so the first line is 1 4. The new table has 3 rows and 2 columns.

Example 2. Input:

3 1
7
8
9

gives:

7 8 9

Why: the table has only one column: 7, 8 and 9. So it becomes one line.

Input

  • Line 1: two whole numbers with a space between them: the number of rows, then the number of columns. Each is from 1 to 10.
  • Then one line for each row. Each row has as many whole numbers as there are columns, with one space between them. The numbers can be negative.

Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line for each column of the input table, with one space between the numbers. The tests try 8 different inputs, also a table with one row, one column, or one number.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 12 · Working With Strings
solution.py
# write your code here

EXPECTED OUTPUT

11 4
22 5
33 6

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.