Generation

generate functionWed, 15 Mar 2023

Rotate and reverse Z John went to a carnival where he came across a stall who provide you gifts if you could solve their challenge. The challenge says that you will be given a matrix and you are supposed to rotate it by 90 degrees clockwise and then traverse the matrix in reverse 'Z' pattern. Help John win a gift by solving th challenge. Note: The 90 degrees clockwise rotation of a matrix 123 741 -> 852 456 789 963 The reverse 'Z' traversal of the matrix is the traversal of matrix in the shape of reverse 'Z L.e. moving in top row from right to left, then moving across the primary diagonal and finally moving along the last ro from right to left. Complete the function rotateReverse which receives n and input matrix as parameters and prints the reversed 'Z' traversal of the 90 degrees rotated matrix. Input Format The first line of input contains a single integer a representing the size of the square matrix The next each of n lines contains n space-separated integers representing the elements of the matrix. Output Format The only line of output contains space-separated integers representing th degrees clockwise rotated matrix

def rotateReverse(n, matrix): # Write your code here # Rotate the matrix 90 degrees clockwise # transpose the matrix and then reverse each row for i in range(n): for j in range(i, n): matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] # reverse each row for i in range(n): matrix[i].reverse() # traverse the matrix in 'Z' pattern for i in range(n): for j in range(n - i): print(matrix[i][j], end=' ') for j in range(i): print(matrix[n-j-1][n-i], end=' ') for i in range(n-1, 0, -1): for j in range(n - i): print(matrix[i][j], end=' ') for j in range(i): print(matrix

Questions about programming?Chat with your personal AI assistant