Tag Archives: Pascal

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;
Console.WriteLine();

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);
firstLetter=firstLetter.PadLeft(mid+1);
Console.WriteLine(firstLetter);

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.Clear();

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

Console.ReadKey();
}
}
}

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!

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 http://pastebin.com/