To Be Or Not To Be

13Oct13

In the twelve months from March 2013 to March 2014, I programmed solutions to the problems posted on the Contest Coding blog run by Lewis Cornwall, solving 33 problems (out of 47) and achieving a position of 4th on the leaderboard (out of 23).

As that blog has now been discontinued, I’m posting here the solutions I programmed to those problems.

To Be Or Not To Be

Rewrite the following passage such that every word is ordered in alphabetical order – while discarding punctuation (“Hello, World” would be translated as “eHllo, dlorW” for example):

To be or not to be – that is the question:
Whether ’tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
And, by opposing, end them.

Solution and answer (To Be Or Not To Be.pas):

Uses: lib_seal_toLowercase.

{$R+}
program To_Be_Or_Not_To_Be( output );
{
Solution and answer for problem "To Be Or Not To Be" (13th October 2013) of http://ContestCoding.WordPress.com/

oT be or not ot be – ahtt is eht einoqstu
eehhrtW ’ist belnor in eht dimn ot effsu
ehT gilnss adn aorrsw fo aegoorstuu efnortu
Or ot aekt amrs aaginst a aes fo belortu
Adn, by ginoopps, den ehmt

Solution programmed in Pascal using Metrowerks CodeWarrior IDE 2.1 (Discover Programming Edition); solution took ~1s to run on a 80MHz PowerPC 601.
}
uses
  lib_seal_toLowercase;

procedure p_SortCharacters( var p : string );
var
  sorted : boolean;
  i : 1..maxint;
  c : char;
begin
  if length( p ) >= 2 then
    repeat
      sorted := false;
      for i := 1 to length( p ) - 1 do
        if ord( f_ToLowercase( p[ i ] )) > ord( f_ToLowercase( p[ i + 1 ] )) then begin
          sorted := true;
          c := p[ i ];
          p[ i ] := p[ i + 1 ];
          p[ i + 1 ] := c
        end
    until not sorted
end;

var
  f : text;
  word : string;
  c : char;
begin
  assign( f, 'To Be Or Not To Be.txt' );
  reset( f );
  word := '';
  while not eof( f ) do begin
    read( f, c );
    if eoln( f ) then begin
        p_SortCharacters( word );
        write( word );
        word := '';
        readln( f );
        writeln
      end
    else
      if f_ToLowercase( c ) in [ 'a'..'z' ] then
        word := word + c
      else begin
        p_SortCharacters( word );
        write( word );
        word := '';
        write( c )
      end
  end;
  close( f )
end.
Advertisements


No Responses Yet to “To Be Or Not To Be”

  1. Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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


%d bloggers like this: