<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tsukasa's ramblings &#187; golf</title>
	<atom:link href="http://blag.tsukasa.net.au/category/programming/golf/feed/" rel="self" type="application/rss+xml" />
	<link>http://blag.tsukasa.net.au</link>
	<description>Tech ramblings of Greg Darke, a University of Sydney student</description>
	<lastBuildDate>Fri, 28 May 2010 12:40:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NCSS Python Golf</title>
		<link>http://blag.tsukasa.net.au/2010/01/03/ncss-python-golf/</link>
		<comments>http://blag.tsukasa.net.au/2010/01/03/ncss-python-golf/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 13:49:52 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[NCSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[python golf]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=159</guid>
		<description><![CDATA[It is time again for me to run a Python Golf challenge&#8230; The aim? To write a python program that solves a problem in the least number of bytes (of source code).
The first of a few problems is:
Given a list of words on stdin (one per line), find the words that have the largest number [...]]]></description>
			<content:encoded><![CDATA[<p>It is time again for me to run a Python Golf challenge&#8230; The aim? To write a python program that solves a problem in the least number of bytes (of source code).</p>
<p>The first of a few problems is:</p>
<p>Given a list of words on stdin (one per line), find the words that have the largest number of <a href="http://en.wikipedia.org/wiki/Anagram">anagrams</a> in that list.</p>
<p>Print all of the words that have the meet the criteria of having the largest number anagrams (One per line, in alphabetical order).</p>
<p>This competition has now finished. The winner was Nick Cooper at 103 bytes, with the following awesome solution:</p>
<pre class="brush: python;">
import sys
s=sorted
o=s(sys.stdin)
r=map(s,o)
d=map(r.count,r)
for e,t in zip(o,d):print e*(max(d)==t),
</pre>
<p><!-- more --></p>
<p>Input:<br />
<code>caret<br />
crate<br />
react<br />
trace<br />
ester<br />
reset<br />
steer<br />
terse<br />
organ<br />
groan</code></p>
<p>Output:<br />
<code>caret<br />
crate<br />
ester<br />
react<br />
reset<br />
steer<br />
terse<br />
trace</code></p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2010/01/03/ncss-python-golf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PyGolf &#8211; Winner of competition 1</title>
		<link>http://blag.tsukasa.net.au/2008/11/29/pygolf-winner-of-competition-1/</link>
		<comments>http://blag.tsukasa.net.au/2008/11/29/pygolf-winner-of-competition-1/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 12:15:27 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=55</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>Well the deadline has come and passed, and we have a clear winner. That winner is <a rel="friend met" href="http://katharos.id.au/blog/">Katie</a>. Her solution comes in at a tiny 121 characters, with the closest solution coming from Tim with 187 characters.</p>
<p>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 <code>str.split</code>.<br />
So without much more talking, here is the winning solution.<br />
<!-- File:081129_katies_winning_solution.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimPreProc">import</span> sys
s=sum(([l[0]]*int(l[-1])<span class="VimStatement">for</span> l <span class="VimStatement">in</span> map(str.split,sys.stdin)<span class="VimStatement">if</span> l[-1]!=<span class="VimNormal">'</span><span class="VimConstant">-</span><span class="VimNormal">'</span>),[])
<span class="VimStatement">for</span> x <span class="VimStatement">in</span> set(s):<span class="VimStatement">print</span> x,s.count(x)</pre>
</div>
<p>It is also interesting to note how she uses the sum function to append a set of lists together.</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/11/29/pygolf-winner-of-competition-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Python golf competition</title>
		<link>http://blag.tsukasa.net.au/2008/11/23/python-golf-competition/</link>
		<comments>http://blag.tsukasa.net.au/2008/11/23/python-golf-competition/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 13:21:46 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=53</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>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 &#8212; their choice.</p>
<p>The problem this week is one of parsing log files. You must parse a log file in the <a href="http://httpd.apache.org/docs/2.2/logs.html#common">common log format</a> 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:</p>
<p><!-- File:081124_sample_solution.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimComment">#!/usr/bin/env python2.5</span>
<span class="VimComment"># Copyright 2008 Greg Darke &lt;greg+laptop@tsukasa.net.au&gt;</span>
<span class="VimComment"># Licensed for distribution under the GPL version 2, check COPYING for details</span>
<span class="VimComment"># Sample program to show the output format for the python golf problem posed at</span>
<span class="VimComment"># <a href="http://blag.tsukasa.net.au/2008/11/23/python-golf-competitionpython-golf-competition/">http://blag.tsukasa.net.au/2008/11/23/python-golf-competitionpython-golf-competition/</a></span>

<span class="VimPreProc">import</span> re, sys

<span class="VimStatement">def</span> <span class="VimIdentifier">main</span>():
    clf_regexp = re.compile(<span class="VimNormal">r'''</span><span class="VimConstant">^(\S+)\s(\S+)\s(\S+)\s\[([^\]]*)\]\s&quot;([^&quot;]*)&quot;\s(\d*)\s(\d*)$</span><span class="VimNormal">'''</span>)
    mapping = {}

    <span class="VimStatement">for</span> line <span class="VimStatement">in</span> sys.stdin:
        m = clf_regexp.match(line)
        <span class="VimStatement">if</span> <span class="VimStatement">not</span> m:
            <span class="VimStatement">continue</span>
        ip, _, _, _, _, _, size = m.groups()
        size = int(size)

        <span class="VimStatement">if</span> ip <span class="VimStatement">not</span> <span class="VimStatement">in</span> mapping:
            mapping[ip] = 0

        mapping[ip] += size

    <span class="VimStatement">for</span> ip <span class="VimStatement">in</span> mapping:
        <span class="VimStatement">print</span> <span class="VimNormal">'</span><span class="VimConstant">%s %d</span><span class="VimNormal">'</span> % (ip, mapping[ip])

<span class="VimStatement">if</span> __name__ == <span class="VimNormal">&quot;</span><span class="VimConstant">__main__</span><span class="VimNormal">&quot;</span>:
    main()</pre>
</div>
<p>Solutions will be accepted until 11:59:59pm(EDT) on Friday 28th November 2008.</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/11/23/python-golf-competition/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Round two of golf</title>
		<link>http://blag.tsukasa.net.au/2008/08/22/round-two-of-golf/</link>
		<comments>http://blag.tsukasa.net.au/2008/08/22/round-two-of-golf/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 10:05:18 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/2008/08/22/round-two-of-golf/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://morepypy.blogspot.com/2008/06/list-comprehension-implementation.html">how list comprehensions are implemented in python</a>, and this sparked another piece of code.</p>
<p>What you see below is actually a <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">prime sieve</a> 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 <code>1e3</code> instead of the longer <code>1000</code>.</p>
<p>Please feel free to leave a comment if you either have a shorter version, or an idea on how to make it shorter.</p>
<p><!-- File:080822_golf_1.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimStatement">print</span>[n <span class="VimStatement">for</span>(n,l)<span class="VimStatement">in</span>[((i,locals()[<span class="VimNormal">'</span><span class="VimConstant">_[2]</span><span class="VimNormal">'</span>]) <span class="VimStatement">if</span> i&gt;1 <span class="VimStatement">else</span>(0,[]))<span class="VimStatement">for</span> i <span class="VimStatement">in</span> range(1e3)]<span class="VimStatement">if</span> n <span class="VimStatement">and</span>(<span class="VimStatement">not</span> l.__setitem__(slice(n*2,None,n),[(0,None)]*len(l[n*2::n])))]</pre>
</div>
<p>Edit: After a little trip home on the bus, I have it down to 132 characters ^^.<br />
<!-- File:080822_golf_2.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimStatement">print</span>[n <span class="VimStatement">for</span>(n,l)<span class="VimStatement">in</span>[(i,vars()[<span class="VimNormal">'</span><span class="VimConstant">_[2]</span><span class="VimNormal">'</span>])<span class="VimStatement">for</span> i <span class="VimStatement">in</span> range(2,1e3)]<span class="VimStatement">if</span> n <span class="VimStatement">and</span>(l.__setitem__(slice(n*2-2,None,n),[(0,0)]*len(l[n*2-2::n]))!=0)]</pre>
</div>
<p>Edit: Make that 128 characters<br />
<!-- File:080822_golf_3.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimStatement">print</span>[n <span class="VimStatement">for</span>(n,l)<span class="VimStatement">in</span>[(i,vars()[<span class="VimNormal">'</span><span class="VimConstant">_[2]</span><span class="VimNormal">'</span>])<span class="VimStatement">for</span> i <span class="VimStatement">in</span> range(1e3)]<span class="VimStatement">if</span> n&gt;1 <span class="VimStatement">and</span>(l.__setitem__(slice(n*n,None,n),[(0,0)]*len(l[n*n::n]))!=0)]</pre>
</div>
<p>Edit: 84 characters, though it is no longer a one line/one statement thing<br />
<!-- File:080822_golf_4.py -->
<div class="codeBlock">
<pre class="VimPre">z=range(1e3);p=(n <span class="VimStatement">for</span> n <span class="VimStatement">in</span> z <span class="VimStatement">if</span> n&gt;1)<span class="VimStatement">for</span> n <span class="VimStatement">in</span> p:z[n*n::n]=[0]*len(z[n*n::n]);<span class="VimStatement">print</span> n</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/08/22/round-two-of-golf/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>First Post</title>
		<link>http://blag.tsukasa.net.au/2008/08/15/first-post/</link>
		<comments>http://blag.tsukasa.net.au/2008/08/15/first-post/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 14:44:34 +0000</pubDate>
		<dc:creator>Tsukasa</dc:creator>
				<category><![CDATA[golf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blag.tsukasa.net.au/?p=3</guid>
		<description><![CDATA[Ok&#8230; So, I couldn&#8217;t resist the temptation of having a title of first post.
Why? Well, because I can&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>Ok&#8230; So, I couldn&#8217;t resist the temptation of having a title of first post.</p>
<p>Why? Well, because I can&#8217;t be bother to do it anywhere else on the internet, so I thought I would use it here.</p>
<p>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).<br />
<!-- File:080815_primes.py -->
<div class="codeBlock">
<pre class="VimPre"><span class="VimStatement">print</span>[x <span class="VimStatement">for</span> x <span class="VimStatement">in</span> range(2,1e3)<span class="VimStatement">if</span> all(x%_ <span class="VimStatement">for</span> _ <span class="VimStatement">in</span> range(2,x))]</pre>
</div>
<p>What does this do? Well you run it for yourself (or are feeling adventurous, you could work it out by hand).</p>
]]></content:encoded>
			<wfw:commentRss>http://blag.tsukasa.net.au/2008/08/15/first-post/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
