Click here to Skip to main content
15,888,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
# We have given a matrix and position of square N*N submatrix,
# We have to rotate that submatrix by 90 degree in clockwise direction
# And, then print whole matrix
# For example, Input matrix is,
# 1 2 3 4
# 5 6 7 8
# 9 10 11 12
# 13 14 15 16
# Position (x, y) = (1, 1) and Value of N = 3
# Output is,
# 1 2 3 4
# 5 14 10 6
# 9 15 11 7
# 13 16 12 8

What I have tried:

I have tried in python but it is applicable only for when x and y are same.
def rotate_submat_90_clk(A, x, y, N):
    for i in range(N//2):
        for j in range(i, N-i-1):
            temp = A[i+y][j+x]
            A[i+y][j+x] = A[N-j-1+x][i+y]
            A[N-j-1+x][i+y] = A[N-i-1+y][N-j-1+x]
            A[N-i-1+y][N-j-1+x] = A[j+x][N-i-1+y]
            A[j+x][N-i-1+y] = temp
    for i in range(len(A[0])):
        print(A[i])
Posted
Updated 8-Jan-20 10:47am

1 solution

Quote:
I have tried in python but it is applicable only for when x and y are same.

Because you are swapping x and y, sometimes x is used as row, sometimes it is a column, and vice versa for y.
You need to choose which one is a row number and the other a column number and not change in middle of code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900