Transpose of a matrix

 Write a program to find the transpose of a given n*m matrix.

Input Format:

  • First line take an Integer from stdin  which is number of rows n.
  • Second line take an Integer from stdin which is number of columns m.
  • Third line take an Integers which are matrix elements

Output Format:

Transpose of a matrix

Example Input:

1   2   3
4   5   6
7   8   9


Output:

1   4   7
2   5   8
3   6   9



Solution:




rows=int(input())  #transpose of a matrix

cols=int(input())

a=list(map(int,input().split()))

k=0

arr= [[0 for i in range(cols)] for j in range(rows)]

for i in range(rows):

    for j in range(cols):

        arr[j][i]=a[k]

        k=k+1

for i in range(rows):

    for j in range(cols):

        print(arr[i][j],end=" ")

    print()




Comments

Popular posts from this blog

Right Arrow Pattern

Efficient Janitor

X Pattern