Saturday I attended Oslo Coding Dojo‘s “Coderetreat summer 2012“. The event was hosted by Steria, and organized by Johannes Brodwall and Anders Karlsen. At 08:30 in the morning, 25 coders showed up at the Steria offices. The day started with breakfast and a brief discussion about the rules for Test Driven Development (TDD), for pair programming and for the task ahead.
The rules for TDD:
- Write no code without first having a test for it
- Only write enough test to red
- Only write enough code to green
- Refactor on green
The day was split into 45 minute sessions, each time with a new partner, each time preferably with a different programming language, and immediately deleting all code when the session ended. Between sessions, we had discussions around what we had learned so far – regarding about TDD, about pair programming, about the various programming languages and about the problem we were working on.
The problem was the same for all sessions; writing a routine print_diamond, thattakes a character as input, and e.g. for ‘D’ prints the following:
During the day, we had a total of six programming sessions, and I got to try solving this problem in C#, Java and Clojure. We also had a lunch break with pizza, and a social gathering at Kristiania ending the event. I really wish more of my colleagues would participate in events like this.
Having been to code dojos before, I know that it’s not a scary thing: you don’t need to be the world’s best programmer, and you don’t even have to know the language you pair program in (at least not if the other guy knows it). I wish more programmers would discover this! Coding dojos are fun and social events, and you learn a lot from participating in them.
I’m a huge fan of pair programming, but maybe not for every task (some tasks are more efficiently solved alone). Pair programming issocial & fun, you have very productive periods of intense effort without distractions, and you learn a lot, both as the “driver” and as the “navigator”. Pair programming is an excellent way to learn (or learn more about) languages new to you, tips & tricks of both the language and the programming environment, and also gives you a lot of room to discuss the best way to solve the problem at hand.
We soon realized that there are several strategies for solving this problem; the brute-force way of calculating leading and intermediate spaces line by line (which most of us tried in the first session) probably being the least efficient.
The strategy I used in most of the sessions was based on the idea that all the lines are actutally the same line – but with all characters except for one masked away. Thus, the above example really looks like this (before the masking, lowercase letters showing characters to be masked away):
After several sessions with this, I realized that I could probably squeeze this into a very short (and almost unreadable) code. I couldn’t just leave this problem, and continued for a while after the coding retreat had officiately ended. For a while I hoped to get the main function down to a twitter message. However, the shortest I managed to get this was 159 characters… The shortest version I have that is still somewhat readable looks like this:
private static IEnumerable<char> Range(char start, char stop)
for (var c = start; c <= stop; c++)
yield return c;
static void Main(string args)
var lines = Diamond('F');
foreach (var line in lines)
public static IEnumerable<IEnumerable<char>> Diamond(char endChar)
var d = Range('A', endChar);
var unmaskedLine = d.Reverse().Concat(d.Skip(1));
var maskChars = d.Concat(d.Reverse().Skip(1));
return maskChars.Select(c => unmaskedLine.Select(x => x == c ? x : ' '));
(Please note that this code is optimized for source code size, not spead or effiency.)