![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Next: grid rotation
What would be your Pythonic solution for rotating a 3x3 grid counterclockwise by 90 degrees?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 211
Rep Power: 3
![]() |
from a strictly code based solution I'd do something like
grid=[[grid[0][2],[grid[1][2],[grid[2][2]],[grid[0][1],[grid[1][1],[grid[2][1]],[grid[0][0],[grid[1][0],[grid[2][0]]] I dunno about a more programatic solution though. -MBirchmeier |
|
|
|
|
|
#3 |
|
Expert Programmer
|
I know this isn't the Pythonic answer you're looking for, but it's an improvement:
for i in xrange(len(grid)):
for j in xrange(len(grid)):
new_grid[i][j] = grid[j][abs(len(grid) - 1 - i)] |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
Pythonic? That would be a simple implementation of an inverse function, using list comprehensions. We will need to count down from the max value, since Python counts backwards (if we are considering the cartegean plane).
print [[grid[y][l-x] for y in range(h)] for x in range(w)] Where w is the width, h is the height, and l = w-1 (defined for efficiency purposes). |
|
|
|
|
|
#5 |
|
Expert Programmer
|
I think the idea is to create a new grid (or modify the original grid), rather than just print the values; this is a little harder. But I may be mistaken.
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
Err... am I missing something? @_@ ...
Chapter 1 of the Python tutorial?? You can replace "print" with "grid =" ... ...--- (Assignment takes place after calculation. It doesn't keep assigning as it's going, so you don't need to worry about that. ) |
|
|
|
|
|
#7 |
|
Expert Programmer
|
Oh, sorry, missed that.
![]() In that case, that's probably the best way to do it. It's simple, concise, and easy to read. Although I wouldn't define a separate variable for w-1, but that's just my preference. |
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
Well, it's usually a poor choice to blatantly disregard simple optimization... especially when all it calls for is swapping a calculation for a variable.
![]() I know it barely makes any difference at all, but it would if say, w-1, was instead a function call. I realise in that case, you would have done it differently, but it's always best to be consistent with your style and make good habits. Although I am being hypocritical, since, in respect to commenting ... I would never be consistent and comment every single program I make. ![]() |
|
|
|
|
|
#9 |
|
Professional Programmer
|
I would use matrix math and apply a transformation matrix to it.
|
|
|
|
|
|
#10 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Python] Grid encryptor | Jessehk | Show Off Your Open Source Projects | 7 | Nov 29th, 2007 7:11 PM |
| Distance between 2 grid references | Oddball | Show Off Your Open Source Projects | 7 | Mar 12th, 2006 2:52 PM |
| easiest way to represent a grid? | keweedsmo | C++ | 4 | Feb 10th, 2006 6:03 PM |
| Little off-topic... Grid coordinate handling | CodeCaster | C++ | 13 | Jul 4th, 2005 7:07 AM |
| Need assistance with program | DJ_Mittens | C++ | 4 | Apr 20th, 2005 7:48 AM |