I recently did problem 1.7 Rotate Matrix in Cracking the Coding Interview. I realized my code is very different than what is in the book and also from what I'm finding online (I'm having trouble following the code in the book). I just want to know if the code I wrote would be appropriate in an interview setting or would I be expected to write code like the solution in the book.
The problem states
Rotate Matrix: Given an image represented by an N×N matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
Here is my code:
def transpose(the_array):
res = []
for i in range(len(the_array)):
transposed = []
for col in the_array:
transposed.append(col[i])
transposed.reverse()
res.append(transposed)
return res
print(transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
Also, would the reverse
method be ok to use in an interview?
I just realized my solution may not be considered in place.