Answer to Question #193681 in Python for Naren

Question #193681

You are given a square matrix A of dimensions NxN. You need to apply the below given 3 operations on the matrix A.


Rotation: It is represented as R S where S is an integer in {90, 180, 270, 360, 450, ...} which denotes the number of degrees to rotate. You need to rotate the matrix A by angle S in the clockwise direction. The angle of rotation(S) will always be in multiples of 90 degrees.


Update: It is represented as U X Y Z. In initial matrix A (as given in input), you need to update the element at row index X and column index Y with value Z.

After the update, all the previous rotation operations have to be applied to the updated initial matrix.


Querying: It is represented as Q K L. You need to print the value at row index K and column index L of the matrix A.


1
Expert's answer
2021-05-15T07:56:38-0400
# the separator of all input values in the program is a space
def get_mat(size):
    res = [[]*size]*size
    for row in range(size):
        res[row] = list(map(float, input(f'Enter {row+1} row matrix:').split()))
    return res


def rotate_right_mat(mat):
    size = len(mat)
    res = []
    for row in range(size):
        res.append([])
        for col in range(size):
            res[row].append(mat[size-1-col][row])
    return res 


# initial input of matrix values    
n = int(input('Enter the dimension of the matrix'))
A =  get_mat(n)
s = int(input('Enter the number of degrees to rotate:'))//90
for _ in range(s):
    A = rotate_right_mat(A)
row, col, value = input('Enter row, column and new value of array element').split()
A[int(row)][int(col)] = float(value)
for _ in range(s):
    A = rotate_right_mat(A)
row, col = input('Enter row, column of array element to display').split()
print(A[int(row)][int(col)])






     

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