Answer to Question #210068 in Python for sudheer

Question #210068

input:4 4 output :[13, 9, 5, 1, 2, 3, 4, 8, 12, 16, 15, 14]

[[13, 9, 5, 1], [14, 6, 7, 2], [15, 10, 11, 3], [16, 12, 8, 4]]

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

3

the ouput should be like this:

13 9 5 1

14 7 11 2

15 6 10 3

16 12 8 4

can anyone give the code correct?


1
Expert's answer
2021-06-23T17:05:36-0400
M, N = input().split(' ')
M, N = int(M), int(N)
matrix = []
for _ in range(M):
	row = [int(x) for x in input().split(' ')]
	matrix.append(row)

K = int(input())

values = matrix[0][:-1] + [x[-1] for x in matrix][:-1] + matrix[-1][::-1][:-1] + [x[0] for x in matrix][::-1][:-1]

values = values[-K:] + values[:-K]

output = matrix

idxs = [(0, j) for j in range(N)][:-1] + [(i, N - 1) for i in range(M)][:-1] + [(M - 1, j) for j in range(N)][::-1][:-1] + [(i, 0) for i in range(M)][::-1][:-1]

idx = 0
for i, j in idxs:
	output[i][j] = values[idx]
	idx += 1

for i in output:
    for j in i:
        print(j, end=' ')
    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