Category: python

Multiplayer Snake

Posted by on January 5, 2009

Well, here we have yet another challenge… Though this one does not involve any prizes, as I want my students at the NCSS camp to actually do their projects and not spend all their time on my competition.

So, what is has to be done here? Well it is quite simple really… Design a bot to play snake for you. I am forcing people who submit to write their solution in python (though there is no real need, other than I am teaching python at the moment). The input/output of the program is quite simple… The output consists of one of the following four letters: ‘U’, ‘D’, ‘L’, ‘R’ (corresponding to Up, Down, Left and Right).

The input consists of the following:
width height snake_id
board layout

An example is:
7 7 A
..*....
..A....
..a....
..a....
.......
.......
..Bbbb.

Where * is an apple, . is an empty cell, the uppercase letters are the head’s of the snakes, with the lowercase being the body.

Code for the engine is located in my repository under the snake project. Submissions should be made to my email address, or in person.

The initial competition will have bots go up against each other, with no time limit given per move. The later rounds will ensure that all bots get equal cpu time (by running the faster bots more often).

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.

Recursive tuples

Posted by on September 26, 2008

Now,
I have been thinking about this for a few days now… Is it possible to create a tuple in python that refers to itself. I don’t mean via some other object, so the following does not count:

def recursive():
    l = []
    t = (l,)
    l.append(t)
    return t

I believe it is possible to do directly from C, but I can not think of a way to do it from within python.

The idea of creating a recursive tuple was spawned from this little comment I found in the pickle source code: “… recursive tuples are a rare thing”. At first I thought it was talking about a tuple that directly refers to itself, but then figured that it must be talking about tuple’s that indirectly refer to themselves.

Significant whitespace

Posted by on September 18, 2008

One of the most common complains I hear from people about python is that whitespace is significant. I would have to disagree with them, I think that significant whitespace is an excellent feature in a programming language. Though only if it is implemented correctly, which I believe it is not in python.

My major complaint about python is that it allows users to mix both tabs and spaces, and also different amounts of spaces to mark a code block. I believe that python should not allow this. I think a there should be one consistent method of indenting used within a file. I would love if this method was tabs, but I don’t really mind that much if it spaces.

This problem usually occurs when you have multiple people modifying code, over an extended period of time. Each person has their own preferred style of indenting code, and will use it (generally without thinking about it).

A friend of mine recently came across this exact problem, and in an attempt to make the file “sane” used a regular expression to fix the whitespace in the file. This ended up being a major problem, as their had inadvertently changed the meaning of the code, by subtle changing the indentation levels of the code. I came up with the idea of parsing the python program, then outputting the program back from the parse tree (thus ensuring the meaning of the code is unchanged).

After being shown the compiler module, I started playing around with some code to reproduce code from the abstract parse tree that was provided to me. When I went looking for documentation, I found that there is a piece of sample code provided with the python source that does exactly what I was after (After a few bug fixes). This example is called unparse.py (located in the Demo/parser/ directory of the python 2.5.x source code).

I suggest anybody who is trying to fix inconsistent indentation in a python file to look at this program. There are a few things to note though:

  • The code must already work as wanted
  • This program will strip all comments from the program
  • The code will loose all of it’s layout - that is, code that may have been split over multiple lines, will now be over one line

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