Darrell Hawley: Home Page

Friday, May 16, 2008

IronPython in Toledo

I stopped down in Toledo on Tuesday to talk to the Northwest Ohio .NET Usergroup about IronPython. The talk went well, though the demo gods frowned upon me during my presentation. It all boiled down to adding my Python installtion to my IronPython path.

  

import sys
sys.path.append("c:\python25\lib")
import random



I kept getting a TypeError exception which I had not seen while building my demos. As it turns out, the reason I had not seen them was because I had been running them via Visual Studio using Ctrl+F5 (Start without Debugging) effectively masking the exception. During the demo, I was simply hitting F5 which of course displayed my dirty laundry for all to see. This doesn't solve the problem but certainly explains the behavior change. Though I'm not absolutely sure what the root cause is, I'm almost certain the problem is that I was trying to get code written for CPython to run inside IronPython. This definitely merits a bit more research.

Friday, May 02, 2008

Project Euler Problem Number 3

Project Euler problem 3 reads as follows:

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

There are no special Python features here that really require explaining. The logic is also straightforward: find the smallest prime that divides evenly into "number"; reset "number" to number/prime; if prime is greater than the square root of number, you have your answer. Solution works in both Python and IronPython.

def GetNextPrime(allPrimes):
"""Given a sequential list of prime numbers, return
the next prime number
"""
value = allPrimes[-1]
if value == 2:
return 3

while True:
value += 2
for prime in allPrimes:
if value % prime == 0:
break;
else:
return value

def Euler003(number = 600851475143):
# import the square root function, seed the primes list
# and determine what
# the largest possible factor
from math import sqrt
primes = [2,3]
limit = sqrt(number)

# if the last prime number exceeds our limit, we're out
while primes[-1] <= limit:
for prime in primes:
if number == prime:
# if the prime and the number are the same,
# we've found our answer. break out of
# the loop
return number
elif number % prime == 0:
# number isn't prime! largest prime factor
# will be in number/prime.
number /= prime
limit = sqrt(number)
break
else:
# still didn't find number % prime == 0? Maybe
# we need bigger prime numbers
while primes[-1] <= limit:
# get next prime number
primes.append(GetNextPrime(primes))
# if number is divisible by next prime number
if number % primes[-1] == 0:
if number == primes[-1]:
break
number /= primes[-1]
limit = sqrt(number)
break
return number

print Euler003()

Labels: ,

Friday, April 25, 2008

Central Ohio Day of .NET and What I Wish I Said

First off, the Central Ohio Day of .NET last Saturday (better late than never, right) was a very well done conference as expected. Hats off to Mike Wood, Carey Payette and Jim Holmes (in reverse alphabetical order) for all the hard work they did. Events are NOT easy. For a recap - and a reason to attend/support next years event - check out this video by Andy Erickson. Nice work, Andy!

Once specific thing I'd like to point out about the sessions, was the relatively large number of talks on alternative languages (one session, Why Ruby? by Joe O'Brien, wasn't even specific to .NET). Though it's really refreshing to see sessions that aren't C# or VB.NET, I wonder what talks are going to look like in another 5 years. Will C# still be the default language for demonstrating new tools or will a DLR-based language (VB.NET, IronPython, IronRuby, etc) or F# be equally as dominant? Hmmmm...

I'm rambling.

After my introductory talk on IronPython, I started thinking about a lot of things I wish I had said or demonstrated. Of course, I don't know how I was going to cram more stuff into the allotted one hour and 10 minutes but nonetheless, here is the list of "Things I Wish I Said and Did":

    • I didn't explore an IronPython executable or library with Reflector. When you do, the first thing you notice is that there is SIGNIFICANTLY more code with an IP app than with a similar C# app. This would have made for a nice discussion on choosing the right language for a given job.
    • I didn't spend enough time on the functional portion of Python. I did gave an example using the map() function and I did a bit with reduce(), but I didn't have a lambda or filter() example prepared.
    • Did I forget to mention that IronPython 2 is targeting .NET 2.0? Yup, I did. Kind of important if you were going to try to use Linq with IP.
    • What about the standard libraries? Python is known as a "batteries included" language, but how would you understand that if I didn't show you the libraries?

I know there are a couple of more items that I'm just forgetting at the moment, but this list discusses some big ones. Hopefully, my next talk will give me some more time.

Saturday, April 12, 2008

Project Euler Problem 2

Problem number 2 reads "Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million." The resolution to this problem is not quite as simple as the previous one, but it's not far off.

def fib(maxFibNumber):
a, b = 0, 1
while a < maxFibNumber:
if (a % 2 == 0):
yield a
a, b = b, a + b

print sum([x for x in fib(4000000)])



This time we can't rely entirely on list comprehension for the answer. We have to define the fib(maxfibnumber) function that will calculate our Fibonacci sequence and return only even those that are even. I modified a Fibonacci function I found at literateprograms.org to do the job. I have to mention one of the little niceties that makes Python so useable: multiple assignment. Multiple assignment describes the technique of assigning - *binding* in Python - two variables simultaneously. We bind "a" and "b" as soon as we enter the "fib" function and then rebind them at the end of the while loop. This is just another way Python can help us with a simple programming idiom.

Labels: ,

Sunday, April 06, 2008

Taking on Project Euler with Python

Lately a number of posts on Project Euler, a series of predefined mathematical/computer science puzzles, have come to my attention. Bill Wagner has started a series of posts solving the Euler problems in C# and Dustin Campbell has posted one solution in F#. Several of my co-workers at SRT Solutions have been working through solutions in Ruby, Scala and Boo. My contribution will be in Python.

Project Euler problem 1 reads as follow: "Add all the natural numbers below 1000 that are multiples of 3 or 5." Python's list comprehension makes the solution to this problem trivial.

print sum([x for x in range(1,1000) if x%5==0 or x%3==0])



List comprehension is really just shorthand for a loop that sticks a variable into a sequence. In this solution, the loop is represented by the "for x in range(1,1000)" portion of the statement and a condition is added immediately following. The first "x" really just represents any value that is added to the sequence assuming it gets by our condition. Use the built-in "sum(list)" function and we have our answer. Simple, elegant and very readable.

Labels: ,