Python is killer

In the era of software development where programmers don't really program anymore, there are still hang out spots on the web where you can pick up your old memories. I've been spending time solving riddles on ProjectEuler.net. It's been rediculously addictive. I am also taking this opportunity to learn Python, which is a modern dynamic language. Just to demostrate how slick it is, here's the code that reads in a list of words from a text file and counts the number of triangular words in it (Problem 42) :

 f = open("p42.txt").read().split(",")
words = [str.upper(str.strip( w.replace('"', ''))) for w in f]
val = [ sum( [ord(c)-64 for c in w]) for w in words]
trinums = [ (.5*n**2+.5*n) for n in xrange(1, max(val))]
print len( [ n for n in val if n in trinums])

 And this is the code that counts the product of the 10th, 100th, 1,000th, 10,000th, 100,000th, and 1,000,000th digits of an irrational decimal fraction (Problem 40.) Guess how long it is:

res = "".join([str(i) for i in xrange(10**6+1)])
print reduce(lambda x,y: x*y, [ int(res[10**i]) for i in xrange(1, 7)]) 

 Just looking at it gives me a boner.

 

 

 

Posted on 2/5/2008 7:45:00 PM by Haoest

Permalink | Comments (1) | Post RSSRSS comment feed |

Categories: Project Euler | Python

Tags:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

February 7. 2008 09:30

Jennifer

lol boner?

I have no idea about what you just said.

That list of code makes no sense to me@@

If anything I'm trying to just learn how to work with ftp and it can baffle me for hours.

Jennifer us

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

November 19. 2008 09:18