Category: golf

PyGolf - Winner of competition 1

Posted by on November 29, 2008

Well the deadline has come and passed, and we have a clear winner. That winner is Katie. Her solution comes in at a tiny 121 characters, with the closest solution coming from Tim with 187 characters.

The main difference came down to the fact that Katie decided to avoid using the regular expression that was given in the sample, and just parse the lines with str.split.
So without much more talking, here is the winning solution.

import sys
s=sum(([l[0]]*int(l[-1])for l in map(str.split,sys.stdin)if l[-1]!='-'),[])
for x in set(s):print x,s.count(x)

It is also interesting to note how she uses the sum function to append a set of lists together.

Python golf competition

Posted by on November 23, 2008

I have written a few entries on python golf before, but I have now decided to make an official competition from it. The rules are fairly simple: I pose a problem simple problem which must be solved (in python) in the lowest number of characters (where a new line counts as one byte). Solutions may be written for any 2.x version of python (ie, 2.3, 2.4, 2.5 or 2.6), and may use any library found on default install on a Debian machine.

Solutions must be emailed to me. In the case where two people have the same character count, the solution that arrived in my inbox first will be declared the winner (this is to stop people from playing with the date header in the email ^^). The winner will receive a chocolate bar or coffee — their choice.

The problem this week is one of parsing log files. You must parse a log file in the common log format that will be given on to your program on stdin. You must then print on stdout the amount of data that was sent to each ip address (and the ip address). A sample program has been provided:

#!/usr/bin/env python2.5
# Copyright 2008 Greg Darke <greg+laptop@tsukasa.net.au>
# Licensed for distribution under the GPL version 2, check COPYING for details
# Sample program to show the output format for the python golf problem posed at
# http://blag.tsukasa.net.au/2008/11/23/python-golf-competitionpython-golf-competition/

import re, sys

def main():
    clf_regexp = re.compile(r'''^(\S+)\s(\S+)\s(\S+)\s\[([^\]]*)\]\s"([^"]*)"\s(\d*)\s(\d*)$''')
    mapping = {}

    for line in sys.stdin:
        m = clf_regexp.match(line)
        if not m:
            continue
        ip, _, _, _, _, _, size = m.groups()
        size = int(size)

        if ip not in mapping:
            mapping[ip] = 0

        mapping[ip] += size

    for ip in mapping:
        print '%s %d' % (ip, mapping[ip])

if __name__ == "__main__":
    main()

Solutions will be accepted until 11:59:59pm(EDT) on Friday 28th November 2008.

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

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).