Month: August 2008

I can write normal code too

Posted by on August 23, 2008

Now,
I am sure that you are all thinking that I must write horrible code normally in python… But that is incorrect, I can write quite nice code. Below I have placed a “normal” version of a prime sieve. Please note, I usually have a bit of whitespace between functions, but I am unable to at the moment due to some wordpress bugs.

I quite like this code, even though I am using a little trick in that the list the generator is iterating over, is being changed while the generator is still using it.

import math
def primes(maxPrime):
    maxSearch = int(math.sqrt(maxPrime))
    nums = range(maxPrime+1)
    nums[1] = 0 # 1 is not a prime
    primes = (n for n in nums if n) # This line is nasty... Changing the thing
                                    # I am iterating over may cause problems
    for n in primes:
        if n <= maxSearch:
            for i in xrange(n**2, maxPrime+1, n):
                nums[i] = 0 # Mark as non prime
        yield n

def main():
    print ', '.join(str(i) for i in primes(10**6))

if __name__ == "__main__":
    main()

Round two of golf

Posted by on August 22, 2008

Well, here we go again. After my last posting about python golf, one of my friends pointed me to a little gem by the guys who are writing PyPy. This link talks about how list comprehensions are implemented in python, and this sparked another piece of code.

What you see below is actually a prime sieve that is written in a single statement. I have used as many tricks as I could think of to lower the number of characters. Note, this code only works on python 2.5 (as I am using the new ternary operator). I also know that this code give a deprecation warning, that is because I am using 1e3 instead of the longer 1000.

Please feel free to leave a comment if you either have a shorter version, or an idea on how to make it shorter.

print[n for(n,l)in[((i,locals()['_[2]']) if i>1 else(0,[]))for i in range(1e3)]if n and(not l.__setitem__(slice(n*2,None,n),[(0,None)]*len(l[n*2::n])))]

Edit: After a little trip home on the bus, I have it down to 132 characters ^^.

print[n for(n,l)in[(i,vars()['_[2]'])for i in range(2,1e3)]if n and(l.__setitem__(slice(n*2-2,None,n),[(0,0)]*len(l[n*2-2::n]))!=0)]

Edit: Make that 128 characters

print[n for(n,l)in[(i,vars()['_[2]'])for i in range(1e3)]if n>1 and(l.__setitem__(slice(n*n,None,n),[(0,0)]*len(l[n*n::n]))!=0)]

Edit: 84 characters, though it is no longer a one line/one statement thing

z=range(1e3);p=(n for n in z if n>1)for n in p:z[n*n::n]=[0]*len(z[n*n::n]);print n

What do you get when a programmer is bored on the train?

Posted by on August 19, 2008

Well, the correct answer to this is a piece of code that can parse the file Firefox uses to store it’s sessions in. This file is located in ~/Library/Application\ Support/Firefox/Profiles/*/sessionstore.js (on an OS X machine).

Upon initial inspection of this file, I thought it might be in JSON. This proved to be an incorrect guess, as some of the keys for the dictionary are not in quotes. After discovering this, I set about writing a parser… Thinking what better way to parse something than with a recursive decent parser.

The code, for those who are interested, is available from my repository.

First Post

Posted by on August 15, 2008

Ok… So, I couldn’t resist the temptation of having a title of first post.

Why? Well, because I can’t be bother to do it anywhere else on the internet, so I thought I would use it here.

Welcome to those of you who have managed to find my blog, I hope to treat your all with some interesting tid-bits of programming, geeky stuff and a little bit of anything else. My present to people reading the first post here is a little bit of python that we came up with after a game of golf (python golf that is).

print[x for x in range(2,1e3)if all(x%_ for _ in range(2,x))]

What does this do? Well you run it for yourself (or are feeling adventurous, you could work it out by hand).