Sunday 28 July 2013

The Python Challenge using Go

This is something I messed about with in February 2013 and never got around to really starting properly. Maybe turning it into a blog posting will encourage me to see it through to completion.

Background:

A few years ago myself and another programmer got hooked on the Python Challenge. This is a programming challenge designed to be solved in Python which asks the programmer to decipher and solve a range of small but increasingly difficult puzzles in a variety of problem domains. From the source files I've had kicking around since then it looks like I managed to get to challenge 17 before either giving up or getting distracted with something else. I did this mainly as an excuse to learn Python and had a great time doing so. So much so that I was able to start building tools in it and now consider it the language I feel most comfortable with after C#.
I've been reading about the Go programming language for the last year or so but haven't really had the chance to learn or use it in anger. I thought that attempting to solve as much of the Python challenge using it as i could would be a decent learning experience. If it worked for Python, why wouldn't it work for go?

Goals:

1. Learn Go to a reasonable standard.
2. Get comfortable using Git and Github.
3. As with everything I try and do, to become a better programmer.


Rules: 

1. All challenges will be solved in only Go.
2. I will resist, as much as possible, from looking up the answers to challenges and looking up the answers to challenges in the source files I had from when I first attempted the challenge using Python.
3. The source files for all challenges will be published on GitHub for posterity. I'll endevour to write about what I've learnt about Go after publishing and talk about what problems I had in their implementation.


GitHub URL:

https://github.com/Mr-Ev-H/pygochallenge.git

Wednesday 4 April 2012

Something different.

I've been given the very exciting opportunity to join a mobile gaming start-up.

I will be swapping my C#, ASP.net, Winforms and WinDbg for Objective C, iOS, Cocoa and maybe some more C#.

I'll post more when things are finalised but my plan over the next few weeks is to get up to speed on Objective C and Unity 3d. I'll be using this blog to post some of the stuff I've found useful and some of the key sticking points I've had wriggling out from within Microsoft's warm embrace.

Wednesday 9 February 2011

Last.fm Windows 7 Taskbar Integration

It would be nice if Chrome and/or Last.fm had a similar integration with the Windows 7 start bar preview media controls as itunes. It is a mild inconvenience to have to restore Chrome every time I wanted to skip a track.

I would hazard a guess that getting this integration working with a windows app would be trivial, getting it working via a browser plugin might be slightly more complex.

Current multi artist radio: Leonard Cohen, (Smog) and Nick Cave and the bad seeds. Very Gloomy.

Wednesday 21 July 2010

FinalBuilder, XPath, WIX, XML namespaces etc.

A few weeks ago myself and another developer spent a morning scratching our heads trying to work out why we couldn't reference certain elements in Wix installation script using XPath through FinalBuilder. Every combination of element and attribute id's we tried produced an empty result set. The only thing that worked was when we referenced the attribute in question using the inbuilt XPath node() function. By using //node()/node() were were able to address the attributes local to the the first element. In the case of Wix this refers to the project element:

<wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<product id="{cea1706b-2804-4c85-b4eb-083b0394fccc}" name="LongArm" language="1033" version="2.187.0"></product>

Therefore using //node()/node() we are able to skip straight to the first child element of the first root element. From here we were able to use the attribute name of Id and Version to access the attributes we needed to generate an upgrade msi. I realised soon after that node() worked because it is namespace independent whereas our attempts at more fancy XPath queries required namespace resolution. Examples I found on the internet were all applied to xml files either devoid of namespaces or which used named namespaces. My first attempt at solving this issue was therefore to force a name on the namespace:

<wix xmlns:a="http://schemas.microsoft.com/wix/2006/wi">
<product id="{cea1706b-2804-4c85-b4eb-083b0394fccc}" name="LongArm" language="1033" version="2.187.0"></product>

This let me address the attributes I needed by using //a:Wix/a:Product

The first time I attempted to run this through the Wix compiler however I was greeted with a compilation error from visual studio. Candle.exe balked at the new and exciting namespace identifier.

Frustrated I then went back to FinalBuilder to see if there were any hints or tips on the configuration of the Edit XML File element. It was then I noticed the MSXML Parser tab in the Edit XML dialog screen:
I ticked the namespace prefix option and was amazed to see this work first time.

Tuesday 6 July 2010

RPN Calculator in Python.

RPN Calculator to solve first Praxis challenge.
http://programmingpraxis.com/2009/02/19/rpn-calculator/

Fairly happy with it, I don't feel right using the in operator to evaluate the token against a string though, it is a bit of a kludge. The first version of this program used a ValueError exception to parse an operator. Although in my heart of hearts I knew this was a bad thing to do it still felt somehow....cleaner.
import sys, operator

def main():
inplist = list(line.split() for line in sys.stdin.readlines())
functions = {'+':operator.add, '-':operator.sub, '*':operator.mul, '/':operator.truediv}

for expression in inplist:
out = list()
for token in expression:
if token[0] in '01234567890':
out.append(float(token))
else:
rhs = out.pop()
lhs = out.pop()
out.append(functions[token](lhs,rhs))
print out[0]

main()

Python input to lists..

Trying to stream a file into python and then convert each line by splitting on whitespace to a token list for parsing word by word.

I started out fairly verbose.

tokens= list()
lines= sys.stdin.readlines()
for line in lines:
tokens.append(line.split())


And ended up reducing it to a one liner.


inplist = list(i.split() for i in sys.stdin.readlines())