Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

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

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