Rotate Matrix Rings
The below url contains code for Rotate Matrix Rings
https://drive.google.com/file/d/1Kn65dh5pVUlIQShr4w7aHSZVNPPpCASF/view?usp=sharing
In the above url link it contains code while running code this error was showing as below
Enter M:
Traceback (most recent call last):
File "main.py", line 1, in <module>
M = int(input("Enter M: "))
ValueError: invalid literal for int() with base 10: '3 4\r'
And also not coming expected test cases. The test cases are below
Test case 1
Input:-
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
Output:-
13 9 5 1
14 7 11 2
15 6 10 3
16 12 8 4
Test case 2
Input:-
3 4
1 2 3 4
10 11 12 5
9 8 7 6
2
Output:-
9 10 1 2
8 11 12 3
7 6 5 4
We need all test cases can be came when code was run. I want exact outputs for all test cases
Use this code:
M, N = input('Enter M, N: ').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('Enter K: '))
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()
Comments
Leave a comment