Problem: Matrix Rotation
Description: Rotate n*n Matrix by 180 degree
For Example:
Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Output:
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
Solution:
import numpy as np
row = int(input("enter no of rows:"))
col = int(input("enter no of columns:"))
n = list(map(str, input().split(" ")))
m = np.array(n).reshape(row,col)
n= len(m)
l = 0
p = len(m)-1
for i in range(n-int(n/2)):
for j in range(n-1,-1,-1):
if i==j==p==l:
break
m[l][i+p-j], m[p][j] = m[p][j], m[l][i+p-j]
l+=1
p-=1
print(m)
Comments