Programming Challenge – Original Pascal Submission

Earlier this week I set a Programming Challenge using an example from my first year in academia. So far it has attracted 60 or so comments and solutions!

I’m particularly impressed with the range of languages that have been used.

Regular day-to-day offerings in C# are there, C# with LINQ, PowerShell and VBScript. “Vanilla” C, C++, Perl, Ruby and Python aren’t left out; neither are Smalltalk, Haskall, Lisp, clojure, Scala, F#, T-SQL, Informix SQL, SyBase SQL, APL, APL2 and A+. 6502 machine code and Excel are in the submission pot to – extreme diamond plotting!

There are even submissions in Octave and J (references here and here)!

I’ve had offers of solutions in JavaScript and jQuery, hopefully they’ll arrive soon!

I haven’t verified it yet, but here’s one of the J solutions, stunningly terse:

It is written in Iverson’s J programming language.

3 :'(],|.@}:)(|.@}.”_1,.])(AB{~i.Y)(_2<\2#i.Y)}'' ''$~2#Y=.>:y(=i.1:)AB=.a.{~65+i.26′”0

by Tracy Harms

That won me “most cryptic” tweet from Stuart!

Naturally I’m pleased, I wasn’t expecting it to be so popular! Huge thanks to all involved so far!

For the sake of completion, I’ve scanned in the original Pascal submission. Here it is:

Oh dear: notice the scandalous indentation after the FOR loop. Cringe.

If I was to write a C# version of the Pascal code, I might end up with something similar to this:

using System;

namespace diamond
class Program
static void Main(string[] args)
char widest_char;
int next_char, finish_char, wide, range, direction, position, spacelength, loop;

widest_char = Console.ReadKey().KeyChar;

wide = (int)widest_char;
direction = 1;
spacelength = 1;
position = 1;

next_char = 66;
finish_char = 65;

range = 2 * (wide - finish_char);

// Calculate initial left indent
int mid = wide - 65 + 1;

String firstLetter = Char.ToString((char)finish_char);

for (loop = 1; loop < range; loop++) { Console.Write(" ".PadLeft(mid - position)); Console.Write((char)next_char); Console.Write(" ".PadLeft(spacelength)); Console.WriteLine((char)next_char); next_char = next_char + (1 * direction); position = position + (1 * direction); spacelength = spacelength + (2 * direction); // Flip direction when the middle of the diamond is reached if (next_char == wide) direction = -1; } Console.WriteLine(firstLetter); Console.ReadLine(); } } }

Back in 1988, I was reasonably pleased with the Pascal solution. Looking at it now, there are a few things I'd probably change, however hindsight with experience is a wonderful thing.

I can't remember how long it took me to write the Pascal version, probably not very long as I had prototyped the solution using BBC Basic V - a programming language that I had been using since I was 13...some five years before I set eyes on the diamond plotter task! I can't recall exactly, but I'm reasonably sure that the BBC Basic version took me about an hour to get working...

Of course, throwing readability, etc. out of the window, were I thinking about writing a similar solution today in 2009, I might find myself writing something similar to this:

using System;

namespace ConsoleApplication5
class Program
static void Main()
int range = Console.ReadKey().KeyChar - 65;

Console.WriteLine(String.Format("{0}A", "".PadLeft(range)));
int r = 1, dir = 1;
while ((r > 0) && (range>0))
char c = Convert.ToChar(r + 65);
Console.WriteLine(String.Format("{0}{1}{2}{3}", "".PadLeft(range - r),
c, "".PadLeft(r + (r - 1)), c));
if (r == range) dir = -1;
r = r + dir;
Console.WriteLine(String.Format("{0}A", "".PadLeft(range)));


How long did it take me to write a solution today? 30 minutes. 10 minutes of which were spent strangely refactoring for fewer lines of code and terseness. It was interesting writing this version, readability and ability to follow the work-flow came through in the initial version. Then the desire to strip it back to the very basics caught up with me. It's still not perfect, but it does the job. That said, I'm not sure my original Pascal tutor would be hugely happy with it! C'est la vie!

I'll write another post that brings together all the submissions that I've received so far - keep them coming!

Fancy a free Tunnock’s Caramel Wafer?

Colin Mackay invites you to blog about DDD Scotland in exchange for a local delicacy:

A Tunnock’s Caramel Wafer* to anyone who blogs about Developer Day Scotland between now and 20th April 2009. You must send me a link to the blog post and the prize can be collected only at Developer Day Scotland on 2nd May 2009. (*Prize subject to change, but will be a Scottish food delicacy)

There’s plenty of other legalese that applies, your acceptance of this delicacy and subsequent weight gain has nothing to do with us, etc.

Leave your links in comments here at this blog, e-mail Colin or send @scottishdevs a message on Twitter!

Zurich – June 29 – July 3 – WCF Master Class with Juval Löwy

WCF Master Class with Juval Löwy (English)

June 29 – July 3, 2009

Zurich, Switzerland (here, to be exact!)

IDesign and John F. Martin present Master WCF in five intense days with Juval Löwy covering WCF programming, design guidelines, pitfalls, original techniques and best practices.

You will see how to take advantage of built-in features such as service hosting, instance management, asynchronous calls, synchronization, reliability, transaction management, disconnected queued calls, security and the .NET Services Bus. The material presented includes IDesign’s original techniques and utilities and goes well beyond anything you can find in conventional sources. The objective of the class is not to just make you a WCF expert, but also a better software engineer.
the cost of the class is 2995€, which includes lunch and dinner, 3495€ which also include the Hotel (excl. any tax)


Free session on Thursday 2nd July
Free, however registration is required:

Energynet – The Next Boom in Software: Juval Löwy, President of IDesign Inc.

Thursday, July 2, 19:00 – 20:30 PM

More detail:
The ongoing economy unraveling is the defining moment of our time. Many professional developers are fearful for their livelihood, as employers and customers cut and slash development plans, and as economic activity grinds to a halt. But not everywhere. In centers of technical excellence and innovation such as the Silicon Valley, the major players, from investors to industry leaders, are aligning themselves with the next boom in software, a field Juval Löwy calls the Energynet.

Alternative energy covers a wide range, from new cars, to micro renewal sources energy producers, infrastructure upgrades to charge stations and distribution, new power and transformation grids, and integration of billing systems, let alone commercial building and homes modification. And the key for making all that work is software. We simply cannot make the physics or the chemistry substantially better, but we can profoundly integrate systems, iron out inefficiency, automate and vertically
integrate energy trading, production and consumption; and the key to all of that is great software.

This massive new software system is the Energynet, and the analogy to the Internet is a good one? instead of packets and request the Energynet transfers watts and usage data, connecting anything and everything in the energy market. In this unique session, Juval Löwy will present the case for the next boom in software, share personal observation and perspectives, and point out the skills and expertise required of developers that want to not only survive but thrive on the next boom in software.

Programming Challenge!

Twenty years ago, during my first year in academia, my Pascal tutor set us some top-notch assignments.

Your mission:

Write a program which draws a diamond of the form illustrated below. The letter which is to appear at the widest point of the figure (E in the example) is to be specified as input data.

Here’s a scan of the original hand-out:

My original Pascal solution, which I prototyped using BBC Basic V, took less than a page of fan-fold listing paper and was implemented as a console application. 50% of the listing dealt with input validation and “do you want to run the program again?” code! I will convert the example line-by-line into C# for later publication here! Of course, were I to write it today, it should look very different!

I’d be keen to see your solutions, written in your choice of programming language. Novelty value for uniqueness in your choice of programming language may well be rewarded! Procedural, object-oriented, functional, dynamic, verbose, terse…the choice is yours!

“What’s in it for me?” you might ask?
Well, nothing really, a bit of kudos and the feeling of a job well done! However, I will offer the two best/novel UK-based solutions a much-coveted DDD polo shirt (modelled here!). My decision is final, colour may vary, size might not be the same size as you, yada yada, other legalese applies, etc.

Submission by comments here on this post, by e-mail (top right About Me), or via Twitter @camurphy please!

Over to you!

UPDATE: Comments seem to mangle the code formatting, it has been suggested that code is submitted either via e-mail or via