Write a function that rotates a 2-dimensional array clockwise or counter clockwise 90 degrees depending on a given parameter, which I believe was either -1 or 1, which told you which way to rotate it. You are given the 2D array as a parameter as well.
Anonymous
def rotate(mat, dir): n=len(mat) new_mat=[[0 for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): if dir == -1: new_mat[i][j]=mat[j][n-i-1] elif dir==1: new_mat[i][j] = mat[n-j-1][i] return new_mat if __name__ == "__main__": assert rotate([[1,2],[3,4]], -1) == [[2,4],[1,3]] assert rotate([[1,2],[3,4]], 1) == [[3,1],[4, 2]]
Check out your Company Bowl for anonymous work chats.