Quote:
|
Originally Posted by DaWei
What would be your Pythonic solution for rotating a 3x3 grid counterclockwise by 90 degrees?
|
If you will really be dealing with larger grids (ie: 3*3 is just to get sample code) then you might find NumPy, etc worthwhile - there's a
rot90() function which does just that.
If you _are_ using 3*3, you feel numpy is overkill, you need to do this frequently, you need the speed, and you don't want to use one of the reallocating solutions you could just roll your own:
# Modifies a 3*3 list of lists inplace. No, it's not incredibly pretty.
def rot90(grid):
r1 = grid[0]
r2 = grid[1]
r3 = grid[2]
(r1[0], r1[1], r1[2],
r2[0], r2[2],
r3[0], r3[1], r3[2]) =\
(r3[0], r2[0], r1[0],
r3[1], r1[1],
r3[2], r2[2], r1[2]) -T.