The other day I had a coding interview and the following question flummoxed me.
You are given a list which represents a matrix as input to a function.
A = [ [1,2,3], [3,4,5], [6,7,8] ]
Your job is to transform matrix 1 to matrix 2.
- The method is in pure python – not numpy
- The function should handle any size of matrix
- The matrix is a square matrix
On the face of it this looks quite easy. The first thing to remember is that it is a list of lists where all internal lists are the same size. It requires slicing and dicing.
To test the output of the function you can use:
test = [[7,4,1], [8,5,2], [9,6,3]]
Lets define the function first.
I want to make a deep copy of the original matrix A, so I’m going to import ‘copy’. I also need to know the dimensions of the matrix based on the length of the list itself and the length of the row in the list which will be defined as ‘rows’ and ‘cols’. It will be helpful to have an empty list to store the rotated matrix in.
import copy b = copy.deepcopy(A) rows, cols = (len(A), len(A) c = 
The trick is to gather all the ‘cells’ from the first column and make them the first row of the new matrix. Python lists start from Zero, so the first for loop will be for the columns:
for j in range(0, cols):
Next task is to gather the data from each column:
for i in range(rows-1, -1, -1): c.append( a[i][j]) b[j] = c c =  return b
The first ‘for’ statement loops from 0 to 3. In python terms, it is actually 0 to 2 because it starts counting from zero and ends at 2. rather than 1 to 3.
The second ‘for’ loop starts from row – 1. That is 2 in this case. It counts down to 0 in steps of -1. The translation is [start, stop, step]. It is counting from 2 down to zero.
The temporary list is stored in the list variable ‘c’ and the first row of list ‘b’ is changed to the values in ‘c’.
‘c’ is then reset to empty for the next run and assignment.
So when ‘j’ = 0 then ‘i’ will = collect all the values from column 1 over all the rows of ‘a’ and store them in a temporary list, ‘c’. That is the assigned to the first row of ‘b’. So on and so forth through the double ‘for’ iterations.
At the end the list ‘b’ will hold the values for the transformed list, A. this returned to the function call.
B = rotate_matrix(A)
Test the result with rotate_matrix(A) == test. If True, you have succeeded!
You can test this out with lists of different sizes such as :
A = [ [ 1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] ]
test = [ [13,9,5,1], [14,10,6,2], [ 15,11,7,3], [16,12,8,4] ]
- Read and understand the question
- Ask questions from the interviewer to better understand
- What kind of data structure is being used – don’t jump to conclusions
- What data structure answers the question
- Learn more pure python!
A = [[1,2,3], [4,5,6], [7,8,9]] test = [[7,4,1], [8,5,2], [9,6,3]] #a = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]] #test = [[13,9,5,1], [14,10,6,2], [15,11,7,3], [16,12,8,4]] def rotate_matrix(a): import copy rows, cols = (len(a), len(a)) c =  b = copy.deepcopy(a) #Don't mess up the input data for j in range(0, cols): for i in range(rows-1, -1, -1): c.append(a[i][j]) b[j] = c c =  return b c = rotate_matrix(A) if rotate_matrix(A) == c #True YES. False FECK.
Easy when you realise Column 0 reversed becomes Row Zero in the rotated matrix.
Comments and better code are always appreciated!!
Tweet if you like it!Tweet