Round 1 : Sorting Letters

23Aug13

I recently participated in the Contest Coding Cup 2013 organised by Lewis Cornwall, being amongst the six (out of fourteen) participants to progress to the final round, in which I achieved a rank of 6th.

As submissions of solutions to that contest are now closed, I’m posting here the solutions I submitted to that contest.

Problem (Round 1):

Sorting Letters

Sort the following letters in alphabetical order (discarding non-alphabetic characters):

A g ? u D n 9 b q Z – S j P x 8 w h C ! f

Solution (Sorting Letters.pas):

Uses: lib_seal_toLowercase.

{$R+}
program Sorting_Letters( output );

{
Solution and answer for problem "Sorting Letters (Contest Coding Cup 2013 - Round 1)" (23rd August 2013) of http://contestcoding.wordpress.com/

AbCDfghjnPqSuwxZ

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

uses
  lib_seal_toLowercase;

function f_SortLetters( p : string ) : string;
var
  s : string;
  i : integer;
  sortOccurred : boolean;
  l, r : char;
begin
  s := '';
  for i := 1 to length( p ) do
    if f_ToLowercase( p[ i ] ) in [ 'a'..'z' ] then
      s := s + p[ i ];
  repeat
    sortOccurred := false;
    for i := 2 to length( s ) do begin
      l := s[ i - 1 ];
      r := s[ i ];
      if ord( f_ToLowercase( r )) < ord( f_ToLowercase( l )) then begin
        sortOccurred := true;
        s[ i - 1 ] := r;
        s[ i ] := l
      end
    end
  until not sortOccurred;
  f_SortLetters := s
end;

begin
  writeln( f_SortLetters( 'A g ? u D n 9 b q Z – S j P x 8 w h C ! f' ))
end.

Result:

AbCDfghjnPqSuwxZ
Advertisements


No Responses Yet to “Round 1 : Sorting Letters”

  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: