Martin Fowler gave me a hard time for KataTwo, complaining that it was yet another single-function, academic exercise. Which, or course, it was. So this week let’s mix things up a bit.
Here’s an exercise in three parts to do with real world data. Try hard not to read ahead—do each part in turn.
Part One: Weather Data
In weather.dat you’ll find daily weather data for Morristown, NJ for June 2002. Download this text file, then write a program to output the day number (column one) with the smallest temperature spread (the maximum temperature is the second column, the minimum the third column).
Part Two: Soccer League Table
The file football.dat contains the results from the English Premier League for 2001/2. The columns labeled ‘F’ and ‘A’ contain the total number of goals scored for and against each team in that season (so Arsenal scored 79 goals against opponents, and had 36 goals scored against them). Write a program to print the name of the team with the smallest difference in ‘for’ and ‘against’ goals.
Part Three: DRY Fusion
Take the two programs written previously and factor out as much common code as possible, leaving you with two smaller programs and some kind of shared functionality.
Kata Questions
- To what extent did the design decisions you made when writing the original programs make it easier or harder to factor out common code?
- Was the way you wrote the second program influenced by writing the first?
- Is factoring out as much common code as possible always a good thing? Did the readability of the programs suffer because of this requirement? How about the maintainability?
I solved it with Groovy in 12 lines clean code. But the logic was mabye too easy.
Posted by: ClubPenguinCheats | July 02, 2010 at 03:10 AM
Nice little problem - here is my solution in Ruby:
http://blog.emson.co.uk/2010/05/code-kata-four-exercise-a-ruby-solution/
Posted by: emson | May 09, 2010 at 10:45 AM
I did the problems in Python and learned a LOT! thanks! Now I'll just have to do them in Java as that is what I will be forced to use at university.
Posted by: Yann | September 11, 2009 at 12:31 PM
Nice problem. I tried out part two and it was an instructive excercise in several ways.
Posted by: John Page | September 06, 2008 at 02:13 AM
I couldn't find any code worth factoring out!
sed 's/^[ ]*//g' < weather.dat | grep ^[[:digit:]] | sort -n --key=2 | head -n1 | cut -f1 -d' '
sed 's/^[ ]*//g' < football.dat | grep ^[[:digit:]] | awk ' { print ($7 - $9) "\t" $2 } ' | sed 's/^-//g' | sort -n | head -n1 | cut -f2
Posted by: Toby | July 31, 2008 at 08:14 AM
That is a nice Task, I solved it with Groovy in 12 lines clean code. But the logic was mabye too easy.
Posted by: Ivan Dolvich | January 09, 2008 at 05:42 PM
OT: The headings "D" and "L" are the wrong way around in the football table. It's 3 points for a win, 1 for a draw and none for a loss.
Posted by: dj | December 18, 2007 at 04:50 AM