To follow up on the rotate like a vortex code we were doing last week, I've
finished off our solution and thought you may find it interesting:
def rotate_like_a_vortex(matrix):
# Do we need to rotate this iteration?
if len(matrix) <= 1:
return matrix
# Zip transposes an array. A transpose and a flip = a rotate of a 2d array
transposed_array = [list(a) for a in zip(*matrix)]
# Flip the array with list slicing
flipped_array = transposed_array[-1::-1]
# Recurse an extra rotate_like_a_vortex call on each subset (layer)
# of the 2d array
inner_array = rotate_like_a_vortex([z[1:-1] for z in flipped_array[1:-1]])
# replace elements of flipped_array with inner_array
for i, x in enumerate(flipped_array[1:-1]):
x[1:-1] = inner_array[i]
# flipped_array is now completely rotated. Return
return flipped_array
https://www.codewars.com/kata/reviews/58dbcf00ef475e15e70001ab/groups/58ff1d6347ad55b575000c51
Cheers,
Phil.
--
--
To post: [email protected]
To unsubscribe: [email protected]
Feeds: http://groups.google.com/group/python-north-west/feeds
More options: http://groups.google.com/group/python-north-west
---
You received this message because you are subscribed to the Google Groups
"Python North-West" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.