Letter Frequency


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.

Letter Frequency

Find the third most frequently used letter in the following passage of lorem ipsum:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse vitae justo est, sed ultricies lacus. Praesent adipiscing adipiscing sapien, iaculis lacinia lectus suscipit eu. Nullam eget scelerisque mi. In eget fermentum nunc. Etiam eu diam a nunc auctor viverra. Nulla leo nunc, placerat in iaculis ut, convallis ac tellus. Praesent ultrices lacinia facilisis. Vivamus dolor massa, vulputate bibendum egestas sed, euismod sit amet nulla. Aliquam erat volutpat.

Nunc hendrerit euismod massa ut scelerisque. Pellentesque et diam eget mi ultricies aliquam eget vitae augue. Nunc risus turpis, pellentesque nec ornare vel, posuere vitae arcu. Mauris commodo lacinia felis. Praesent feugiat condimentum velit, a euismod nisl euismod et. Suspendisse potenti. Sed eu imperdiet nibh.

Solution and answer (Letter Frequency.pas):

Uses: lib_seal_toLowercase.

program Letter_Frequency( output );
Solution and answer for problem "Letter Frequency" (31st May 2013) of http://ContestCoding.WordPress.com/

's' is the third most frequently used letter (61 uses.)

Solution programmed in Pascal using Metrowerks CodeWarrior IDE 2.1 (Discover Programming Edition); solution took ~1s to run on a 80MHz PowerPC 601.
  t_LetterFrequency = record
    m_letter : char;
    m_frequency : 0..maxint
  i : 1..26;
  lF : array [ 1..26 ] of t_LetterFrequency;
  f : text;
  c : char;
  sortOccurred : boolean;
  temp_lF : t_LetterFrequency;
  for i := 1 to 26 do begin
    lF[ i ].m_letter := chr(( i - 1 ) + ord( 'a' ));
    lF[ i ].m_frequency := 0

  {Read the letters from file.}
  assign( f, 'Letter Frequency.txt' );
  reset( f );
  while not eof( f ) do begin
    read( f, c );
    if eoln( f ) then
      readln( f );
    c := f_ToLowercase( c );
    if c in [ 'a'..'z' ] then begin
      i := ord( c ) - ( ord( 'a' ) - 1 );
      lF[ i ].m_frequency := lF[ i ].m_frequency + 1
  close( f );

  {Sort the letter frequencies.}
    sortOccurred := false;
    for i := 1 to 25 do
      if lF[ i ].m_frequency < lF[ i + 1 ].m_frequency then begin
        sortOccurred := true;
        temp_lF := lF[ i ];
        lF[ i ] := lF[ i + 1 ];
        lF[ i + 1 ] := temp_lF
  until not sortOccurred;

  {Output the third most frequenty used letter.}
  writeln( '''', lF[ 3 ].m_letter, ''' is the third most frequently used letter (', lF[ 3 ].m_frequency, ' uses.)' )

No Responses Yet to “Letter Frequency”

  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: