1. Computing & Technology

Discuss in my forum

Programming Contest 51 - Draw a Series of Squares

Finished. No one was interested!

By , About.com Guide

Grid showing placement of letters
As recent programming challenges have been a little bit over complex in recent times this one should be one of the simplest, as it involves writing code to put letters in a grid. Take a given phrase, in this case

It's a lovely day outside. Fancy a game of tennis.

Fit this into a grid twice, once as a square (it looks rectangular as the cells are rectangular ) flowing clockwise and once as a diamond also flowing clockwise.

It's been fitted into a 13 x 13 square along the edge and in the 21 x 21 square as a diamond shape starting at (1,11) (x,y coordinates, across then down) for the diamond running north east to (11,1) and (5,5) for the square running clockwise. Fit the square in first then the diamond. Basically you should replicate what's been done in the image above.

The Challenge

Fit the above phrase into the square grid first then the diamond grid, except that where there is a letter already you must skip it as if the letter was written. For instance (5,7) has the i of tennis from the square version. It should also have the space character between it's and lovely from the diamond version.

Do this in four runs, but varying the start position each time of the text so that on the first time the square starts at (5,5), the second at (17,5), the 3rd at (17,17) and the last at (5,17). On the same four runs the diamond should start at (1,11) on the first run, (11,1) on the second, (21,11) for the third and (21,11) for the last.

Output

The program must create an output file called output.txt in the same location as your program and output all four grids. This should output a period (full stop for us Brits) for every location where there is no letter so initialize the grid with this.

Notes.

The should be one char per cell. In my case the ' char was removed (I used Excel) so those cells look empty. There should be no caching of the output grids. Each run the grid should be cleared and the letters output. You can use any other technique you wish to save time but it must regenerate the displays each time.

Timing Code

Please time from the start of the first run until the end of the fourth just before the output file is opened for writing. As this is likely to be very fast, please add code to loop multiple times regenerating the four grids so that the total elapsed time is 1-5 seconds. If for instance it took 0.001 seconds to create the four grids once then a loop of 1000 times should take just about a second to run.

This code below will do high speed timing for Windows (first three) and the fourth one for Linux.

Winning Criteria

This is a pure speed contest. Whoever's code can write the letters into the grids or whatever structure you choose the fastest wins.

Rules

This is for glory only. About.com does not permit prizes to be given.

Please submit your source code and the output file to the cplus.guide@about.com?subject=Programming Contest 51 email address with the subject line Programming Contest 10.

It must compile with Open Watcom, Microsoft Visual C++ 2008/2010 Express Edition/Microsoft Visual Studio 2008/2010, CC386 or Borland Turbo C++ Explorer, Microsoft Visual C# 2008/2010 Express Edition or GCC/G++. If it doesn't compile, it can't be run so is automatically disqualified.

Please include your name, age (optional), blog/website url (optional) and country. Your email address will not be kept, used or displayed except to acknowledge your challenge entry. You can submit as many entries as you like before the deadline which is October 31 2011.

The top ten entries will be listed, judged purely on points. A condition of entry is that you allow your source code to be published on this website, with full credits to you as the author.

©2012 About.com. All rights reserved.

A part of The New York Times Company.