Answer to Question #202707 in Python for sudheer

Question #202707

M, N = input().split(' ')

M, N = int(M), int(N)

matrix = []

for i in range(M):

row = [int(j) for j in input().split(' ')]

matrix.append(row)

values = [j for row in matrix for j in row]

values.sort()

output = matrix

for i in range(M):

for j in range(N):

output[i][j] = values[i * M + j]

for row in output:

print(*row)

input:

2 5

-50 20 3 25 -20

88 17 38 72 -10

output:-
50 -20 -10 3 17
-10 3 17 20 25
the output should be like this:-
50 -20 -10 3 17 
20 25 38 72 88


so can anyone please give correct code?


1
Expert's answer
2021-06-03T13:42:14-0400
M, N = input().split(' ')


M, N = int(M), int(N)


matrix = []


for i in range(M):
    row = [int(j) for j in input().split(' ')]
    matrix.append(row)


output=[]
for i in range(M):
    for j in range(N):
        output.append(matrix[i][j])
print()
output.sort()
n=0
for i in range(M):
    for j in range(N):
        print(output[n],end="  ")
        n=n+1
    print()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS