Transpose and Flatten HackerRank Solution

This Python program deals with the manipulation of a 2D array using the numpy library. It first takes the dimensions of the array as input, then the elements of the array. The program then transposes the array, which means it swaps the row and column indices of each element. After that, it flattens the array, which means it converts the 2D array into a 1D array.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Transpose and Flatten HackerRank Solution HackerRank solution using CodeRankGPT:

The solution works by first taking the dimensions of the 2D array as input. It then takes the elements of the array as input and stores them in a list of lists. This list of lists is then converted into a numpy array for easy manipulation. The numpy.transpose function is used to transpose the array, and the numpy.flatten function is used to flatten the array. Both the transposed and flattened arrays are then printed out.


import numpy

n, m = map(int, input().split())
ar = []
for _ in range(n):
    row = list(map(int, input().split()))
    ar.append(row)

np_ar = numpy.array(ar)
print(numpy.transpose(np_ar))
print(np_ar.flatten())

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform