Kerry Buckley

What’s the simplest thing that could possibly go wrong?

Archive for April, 2008

Git quote of the day

leave a comment

From Amy Hoy on Slash7:

If svn is your sometimes catankerous but serviceable steed, git is like a chimera crossed with a unicorn with handy built-in saddle bags plus a sword.

Technorati Tags: , , , ,

Written by Kerry

April 25th, 2008 at 7:18 pm

Posted in Software

On being beaten with carrots

5 comments

CarrotsTraditional approaches to motivation tend to fall into one of two camps: the carrot (“do this and you’ll be rewarded”) or the stick (“do this or you’ll suffer the consequences”).

I guess I’m fortunate to work for a company (and in a country) where by and large there isn’t a culture of firing people who don’t meet performance targets – instead we have a system where an ever-increasing proportion of people’s pay packet is made up of a performance-related bonus, rather than a fixed salary.

So, how do you go about getting your bonus each quarter? Simple: just meet your agreed targets.

Of course, nothing in a large corporation can ever be that simple, so in practice there’s a complex tiered system of company, business unit, programme, project, team and individual targets, which are combined in a magic spreadsheet to generate how much bonus everyone gets. Each of these targets, and the performance against them, has to be agreed, monitored, quantified, audited and levelled. At each stage politics comes into play. Those that enjoy playing systems try to set targets they know they can achieve. People concentrate on meeting the letter of the objectives, possibly to the detriment of other activities, like helping colleagues or making process improvements.

Now step away from this corporate dystopia for a moment, into the world of Agile Software Development. A world where we value Individuals and interactions over processes and tools, and Responding to change over following a plan. A world where we strive to build projects around motivated individuals, give them the environment and support they need, and trust them to get the job done. Where working software is the primary measure of progress. Where at regular intervals, the team reflects on how to become more effective, then tunes and adjusts its behavior accordingly. Where the self-organising team, accountable directly to it customer or product owner, is responsible for its own delivery and processes.

Now, when agile teams in large organisations are forced to jump through the externally-imposed hoops of objectives, development action plans and post-implementation reviews (which add little visible benefit and take up time that could be spent delivering business value), is it any wonder that the carrot of performance-related pay feels like it’s more of a punishment than an incentive?

Technorati Tags: , , , ,

Public domain photo from Wikimedia Commons.

Written by Kerry

April 22nd, 2008 at 1:11 pm

Posted in Agile,Enterprise,Rants

Python II: Fibonacci Sequence

one comment

Tim’s second exercise (here’s the first):

Write a recursive function that calculates the value of Fibonacci numbers. These are your acceptance criteria:

  • Calculating fib(250) must return 7896325826131730509282738943634332893686268675876375
  • The function must use recursion. No intermediary data structures, etc.
  • The implementation must be written in pure python – no C extension modules, that’s cheating.
  • The function must calculate the 250th Fibonacci number in under one second.



    You will get extra points if:
  • You can also demonstrate a proof for the Reciprocal Fibonacci constant, meeting the following conditions
    • Your proof must also run in under one second
    • Your proof must not duplicate any of the concerns addressed by your original Fibonacci function implementation.
    • Your proof is allowed to call into the Fibonacci function though!

The Reciprocal Fibonacci constant is defined as:

P(F) = Sum[k = 1..infinity] 1/F(k) = 3.35988566…

Where F is a Fibonacci number

Here’s a couple of hints about things to look for:

  • Memoization
  • Function decorators

I haven’t tried the extra credit section yet, but this is what I came up with. Again, starting with a naive solution with some doctest tests:

[python]
#!/usr/bin/env python

“””
>>> fib(1)
1
>>> fib(2)
1
>>> fib(3)
2
>>> fib(4)
3
>>> fib(5)
5
“””
def fib(n):
if n <= 2:
return 1
else:
return fib(n-2) + fib(n-1)

def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()
[/python]

Well that seems to work OK. Now, try adding a test for the 250th number:

[python]
>>> fib(250)
7896325826131730509282738943634332893686268675876375
[/python]

And wait … and wait …

No, doesn’t look like that’s going to return any time soon. Of course when you look at what it’s doing, the complexity’s increasing in the order of 2n or something, which isn’t good.

OK, I guess this is where memoisation comes in. After much head-scratching on the train this morning, I came up with this:

[python]
fibs = {}

def fib(n):
if n <= 2:
return 1
elif fibs.has_key(n):
return fibs[n]
else:
fibs[n] = fib(n-1) + fib(n-2)
return fibs[n]
[/python]

A little tweak to allow it to be called from the command line with the value of n passed as an argument, and here’s the final version:

[python]
#!/usr/bin/env python
import sys

fibs = {}

def fib(n):
if n <= 2:
return 1
elif fibs.has_key(n):
return fibs[n]
else:
fibs[n] = fib(n-1) + fib(n-2)
return fibs[n]

def _test():
import doctest
doctest.testmod()

if len(sys.argv) > 1:
print fib(int(sys.argv[1]))
if __name__ == “__main__”:
_test()
[/python]

fib(master) $ time ./fib.py 250
7896325826131730509282738943634332893686268675876375

real	0m0.280s
user	0m0.161s
sys	0m0.047s

Sorted!

Technorati Tags: , ,

Written by Kerry

April 17th, 2008 at 11:49 am

Posted in Software

First stab at Python

4 comments

Monty Python foot “It is an ex parrot. It has ceased to be.”

No, of course not. I’m talking about the other Python. Not the one with the silly walks, dead parrot and singing lumberjacks, but the one with the idiosyncratic approach to whitespace.

One of the APIs available for the Web21C SDK is Python, and there was recently some concern that we didn’t have enough expertise in the team to continue providing technical support for the Python SDK. A bunch of us have volunteered to start learning the language, and as a first step, Tim set us some homework:

Write a program that processes a list of numbers from 1 to 100. For each number, if the number is a multiple of 3, print “FIZZ”; if the number is a multiple of 5, print “BANG”; otherwise, print the number.

You are *NOT* allowed to use any *IF/ELSE* statements in your code. You can use the list-accessing ternary operator hack, but whilst I’ll accept your homework if you do, you’ll miss out on the prize (alcoholic), which goes to the most concise code (not including whitespace).

Now I have no idea what this mysterious ‘list-accessing ternary operator hack’ might be, but it sounds painful (as does missing out on an alcoholic prize), so it looks like I need to eschew ifs completely.

Since I’ve never written a line of Python in my life, I decided the easiest way to proceed would be to start off by figuring out enough syntax to solve the problem as defined by the first paragraph, then write a test which the simple code passes, then factor out the conditional logic. Normally I’d start with the test, but I think learning a new language is one of the cases where TDD isn’t really appropriate.

So here’s my first working code:

[python]
for n in range(1, 100):
if n % 3 == 0:
if n % 5 == 0:
print ‘FIZZBANG’
else:
print ‘FIZZ’
elif n % 5 == 0:
print ‘BANG’
else:
print n
[/python]

This prints out what you would expect:

PyMate r8111 running Python 2.5.1 (/usr/bin/env python)
>>> fizzbang.py

1
2
FIZZ
4
BANG
FIZZ
7
8
FIZZ
BANG
11
FIZZ
13
14
FIZZBANG
16
...

OK, so far so good. The next step was to find out what the Pythonistas use for unit testing. As a card-carrying BDD and RSpec fan, I was initially interested to see that there’s a PySpec, but at first glance it doesn’t look that great (maybe it’s just that I’m not used to reading Python code). Then I came across the bundled DocTest, and while I’m not entirely convinced by its description on the Wikipedia BDD page as ‘BDD for Python’, it looked ideal for the task at hand.

Here’s the same code, but with the addition of a parameter to specify the number to count up to, and a DocTest test:

[python]
#!/usr/bin/env python

“””
Print numbers up to limit, replacing those divisible by 3 and/or 5 with FIZZ
and/or BANG.

>>> fizzbang(20)
1
2
FIZZ
4
BANG
FIZZ
7
8
FIZZ
BANG
11
FIZZ
13
14
FIZZBANG
16
17
FIZZ
19
BANG
“””
def fizzbang(limit):
for n in range(1, limit + 1):
if n % 3 == 0:
if n % 5 == 0:
print ‘FIZZBANG’
else:
print ‘FIZZ’
elif n % 5 == 0:
print ‘BANG’
else:
print n

def _test():
import doctest
doctest.testmod()

if __name__ == “__main__”:
_test()
[/python]

OK, now to start thinking about the hard bit – getting rid of those ifs. As a first step, I got rid of the nested logic by appending the ‘FIZZ’ and/or ‘BANG’ to a temporary string, then either printing that string, or the original number if it was empty:

[python]
def fizzbang(limit):
for n in range(1, limit + 1):
out = ”
if n % 3 == 0:
out += ‘FIZZ’
if n % 5 == 0:
out += ‘BANG’
print out or n
[/python]

At this point, I hit on the idea of using arrays to select either an empty string or the appropriate word:

[python]
def fizzbang(limit):
for n in range(0, limit):
print ['', '', 'FIZZ'][n%3] + ['', '', '', '', 'BANG'][n%5] or n + 1
[/python]

Now all that’s left is to remove the limit parameter so the code merely satisfies the original criteria:

[python]
for n in range(100):
print ['', '', 'FIZZ'][n%3] + ['', '', '', '', 'BANG'][n%5] or n + 1
[/python]

I expect there are other more concise (and no doubt more efficient) solutions – I look forward to seeing what the other ‘pupils’ come up with!

Technorati Tags: , , ,

Written by Kerry

April 14th, 2008 at 8:44 am

Posted in Software

What’s your command-line top ten?

6 comments

Here’s mine:

~ $ history|awk '{a[$2]++} END{for(i in a){printf "%5d\t%s\n",a[i],i}}'|sort -rn|head
  232	git
   82	cd
   30	ls
   20	sudo
   18	rm
   13	./script/server
   12	mate
   11	rake
    7	vi
    7	ssh

Probably slightly skewed by the fact that I was doing a git demo yesterday.

Meme via Simon Brunning.

Written by Kerry

April 11th, 2008 at 2:23 pm