Computer Science, asked by rohangupta738, 8 months ago

A square n×n matrix of integers can be written in Python as a list with n elements, where each element is in turn a list of n integers, representing a row of the matrix. For instance, the matrix
1 2 3
4 5 6
7 8 9
would be represented as [[1,2,3], [4,5,6], [7,8,9]].

Write a function leftrotate(m) that takes a list representation m of a square matrix as input, and returns the matrix obtained by rotating the original matrix counterclockwize by 90 degrees. For instance, if we rotate the matrix above, we get

3 6 9
2 5 8
1 4 7

Answers

Answered by ishika07042000
12

Answer:

def leftrotate(m):  

   N = len(m[0])

   

   for x in range(0, int(N / 2)):  

       for y in range(x, N-x-1):  

             

           temp = m[x][y]  

           m[x][y] = m[y][N-1-x]  

           m[y][N-1-x] = m[N-1-x][N-1-y]  

           m[N-1-x][N-1-y] = m[N-1-y][x]  

           m[N-1-y][x] = temp  

           

   return(m)

Explanation: I hope this helps

Answered by feminasikkanther
0

Explanation:

Provided that:

The given square matrix is:

A = [[1,2,3], [4,5,6], [7,8,9]]

To rotate this matrix we can use Transpose of Matrix;

The driver code to get the transpose of Matrix:

Program Input:

M=int(input("Enter M : "))

N=int(input("Enter N : "))

def transpose(A, B):

for i in range(N):

for j in range(M):

B[i][j] = A[j][i]

print('Insert elemnts of [A]')

a=[int(input()) for i in range(N)]

b=[int(input()) for i in range(N)]

c=[int(input()) for i in range(N)]

print('Matrix A=')

A=[a

,b,

c]

for row in A:

print(row)

print("\n")

print('Order of Matrix [A] :',M,'×',N)

B = [[0 for x in range(M)] for y in range(N)]

transpose(A, B)

print( )

print("Result matrix is [A`] =")

for i in range(N):

for j in range(M):

print(B[i][j], " ", end='')

print()

print( )

print('Order of Matrix [A`] :',N,'×',M)

Program Output:

Enter M : 3

Enter N : 3

Insert elemnts of [A]

1

2

3

4

5

6

7

8

9

Matrix A=

[1, 2, 3]

[4, 5, 6]

[7, 8, 9]

Order of Matrix [A] : 3 × 3

Result matrix is [A`] =

1 4 7

2 5 8

3 6 9

Order of Matrix [A`] : 3 × 3

[Program finished]

Hence by this python programming code we can easily rotate any square matrix (even any matrix) at 90° angle in clockwise direction.

The image of output of this program is given as attachment.

Attachments:
Similar questions