Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 25th, 2006, 3:33 PM   #1
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jul 25th, 2006, 4:08 PM   #2
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 3 MBirchmeier is on a distinguished road
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
MBirchmeier is offline   Reply With Quote
Old Jul 25th, 2006, 7:18 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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)]
titaniumdecoy is offline   Reply With Quote
Old Jul 25th, 2006, 9:42 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
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).
Sane is offline   Reply With Quote
Old Jul 25th, 2006, 9:59 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Jul 25th, 2006, 10:09 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
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. )
Sane is offline   Reply With Quote
Old Jul 25th, 2006, 10:55 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 836
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Jul 25th, 2006, 11:21 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,791
Rep Power: 5 Sane will become famous soon enough
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.
Sane is offline   Reply With Quote
Old Jul 26th, 2006, 12:41 AM   #9
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
I would use matrix math and apply a transformation matrix to it.
andro is offline   Reply With Quote
Old Jul 26th, 2006, 7:29 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
I would use matrix math and apply a transformation matrix to it.
Why? Example?
__________________
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC