Anders G. Nordby

Senior Systems Consultant at Making Waves AS

Coderetreat summer 2012

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:

  1. Write no code without first having a test for it
  2. Only write enough test to red
  3. Only write enough code to green
  4. 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:

  B B
 C   C
D     D
 C   C
  B B

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:

using System;
using System.Collections.Generic;
using System.Linq;

namespace PrintDiamond
    class Program
        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)
                Console.WriteLine(String.Join(string.Empty, line));


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

About these ads

2 responses to “Coderetreat summer 2012

  1. Oddbjørn Kvalsund 2012-05-28 at 00:53

    Anders, below is my recursive Java-version from the Coderetreat.

    public class Diamond {


    public String generateDiamond(char current, char stop) {
    String unmaskedLine = ALPHABET.substring(ALPHABET.indexOf(stop), ALPHABET.indexOf(stop) + (stop-’A’) * 2 + 1);
    String maskedLine = unmaskedLine.replaceAll(“[^" + current + "]“, ” “) + “\n”;

    return (current == stop) ? maskedLine : maskedLine + generateDiamond((char) (current + 1), stop) + maskedLine;

    public static void main(String[] args) {
    Diamond diamond = new Diamond();
    System.out.println(diamond.generateDiamond(‘A’, ‘R’));

  2. Oddbjørn Kvalsund 2012-05-28 at 01:08

    Hm, WordPress decided to mangle my formatting, so here’s a slightly sobered up version on pastebin:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: