Project Euler # 14

questions/discussions

Moderators: KDoiron, ChrJim, mawe, python

Re: Project Euler # 14

Postby WaynaPicchu on Thu Feb 25, 2010 5:45 am

Bill wrote:
WaynaPicchu wrote:Still slower than others have reported. Any other optimization tips?
Yeah -- replace the try/except block with a simple if. I replaced the try/except block with this:
Code: Select all
          v = A.get(k)
          if v:
              cnt += A[k]
              break

The modified program runs in about 5.7 seconds.

Oohhhhhh: "get(key[, default]) Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError."

Didn't know about the get method. That last part is essential. It solves that nasty KeyError problem I had to get around with the try/except block.

Ran in 9 seconds with that modification. Thanks.
WaynaPicchu
New Python User
New Python User
 
Posts: 24
Joined: Sun Dec 20, 2009 5:21 am

Previous

Return to Project Euler

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Dreamlink Web hosting and Traduzioni Rumeno Italiano and ASSP Deluxe for cPanel.