HackerRank Python Solution - Numpy Topic - Transpose and Flatten

Question 2 - Transpose and Flatten

Task

You are given an NxM integer array matrix with space-separated elements (N= rows and M= columns).
The question is to print the transpose and flatten the results.

Input Format

The first line contains the space-separated values of N and M.
The next N lines contain the space-separated elements of M columns.

Output Format

First, print the transpose array and then print the flatten.

Sample Input

    2 2

    1 2

    3 4

Sample Output

    [[1 3]

    [2 4]]

    [1 2 3 4]

Solution:

import numpy

n,m = map(int,input().split())

arr = numpy.array([ list(map(int,input().split())) for _ in range(n)])

print(numpy.transpose(arr),arr.flatten(),sep="\n")

Disclaimer: The problem statement is given by hackerrank.com but the solution is generated by the Geek4Tutorial admin. If there is any concern regarding this post or website, please contact us using the contact form. Thank you!

No comments:

Post a Comment

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...