Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of dgeiger

dgeiger's solution

to Pangram in the Delphi Pascal Track

Published at Aug 30 2020 · 1 comment
Instructions
Test suite
Solution

Determine if a sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will not contain non-ASCII symbols.

Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

Loading Exercises into Delphi

If Delphi is properly installed, and *.dpr file types have been associated with Delphi, then double clicking the supplied *.dpr file will start Delphi and load the exercise/project. control + F9 is the keyboard shortcut to compile the project or pressing F9 will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the File drop down menu.

When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the exercism/delphi/<exerciseName> directory.

For example, if you're submitting ubob.pas for the Bob exercise, the submit command would be something like exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas.

Source

Wikipedia https://en.wikipedia.org/wiki/Pangram

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you may request help from a mentor.

uPangramTests.pas

unit uPangramTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '2.0.0';

type

  [TestFixture('isPangram')]
  TPangramTests = class(TObject)
  public
    [Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
    procedure empty_sentence;

    [Test]
    [Ignore]
    procedure perfect_lower_case;

    [Test]
    [Ignore]
    procedure only_lower_case;

    [Test]
    [Ignore]
    procedure missing_the_letter_x;

    [Test]
    [Ignore]
    procedure missing_the_letter_h;

    [Test]
    [Ignore]
    procedure with_underscores;

    [Test]
    [Ignore]
    procedure with_numbers;

    [Test]
    [Ignore]
    procedure missing_letters_replaced_by_numbers;

    [Test]
    [Ignore]
    procedure mixed_case_and_punctuation;

    [Test]
    [Ignore]
    procedure case_insensitive;
  end;

implementation
uses SysUtils, uPangram;

{ PangramTests }

procedure TPangramTests.empty_sentence;
begin
  Assert.IsFalse(isPangram(''));
end;

procedure TPangramTests.perfect_lower_case;
begin
  Assert.IsTrue(isPangram('abcdefghijklmnopqrstuvwxyz'));
end;

procedure TPangramTests.only_lower_case;
begin
  Assert.IsTrue(isPangram('the quick brown fox jumps over the lazy dog'));
end;

procedure TPangramTests.missing_the_letter_x;
begin
  Assert.IsFalse(isPangram('a quick movement of the enemy will jeopardize five gunboats'));
end;

procedure TPangramTests.missing_the_letter_h;
begin
  Assert.IsFalse(isPangram('five boxing wizards jump quickly at it'));
end;

procedure TPangramTests.with_underscores;
begin
  Assert.IsTrue(isPangram('the_quick_brown_fox_jumps_over_the_lazy_dog'));
end;

procedure TPangramTests.with_numbers;
begin
  Assert.IsTrue(isPangram('the 1 quick brown fox jumps over the 2 lazy dogs'));
end;

procedure TPangramTests.missing_letters_replaced_by_numbers;
begin
  Assert.IsFalse(isPangram('7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog'));
end;

procedure TPangramTests.mixed_case_and_punctuation;
begin
  Assert.IsTrue(isPangram('Five quacking Zephyrs jolt my wax bed.'));
end;

procedure TPangramTests.case_insensitive;
begin
  Assert.IsFalse(isPangram('the quick brown fox jumps over with lazy FX'));
end;

initialization
  TDUnitX.RegisterTestFixture(TPangramTests);
end.
unit uPangram;

interface

function isPangram(TestString: string): Boolean;

implementation

function isPangram(TestString: string): Boolean;
var
  Index: Integer;
  TestLength: Integer;
  Letters: string;
  TestChar: Char;
  CharPos: Integer;
begin
  // First, we'll assume that TestString isn't a pangram
  Result := False;

  // For a quick and dirty test, we can just check the length of TestString
  TestLength := Length(TestString);

  // If TestString doesn't have at least 26 characters, it's impossible for
  // it to contain all 26 letters, so we can just check TestString if there
  // is a possibility that all 26 letters are contained.
  if TestLength > 25 then
    begin
      // We'll use a string of all of the upper-case letters to keep track
      // of which letters we haven't seen yet
      Letters := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

      // Now, for each character in TestString, we get the uppercase version
      // of the letter - to simplify testing - and, if we haven't already seen
      // it, remove it from the string of unseen letters.
      for Index := 1 to TestLength do
        begin
          // Let's get the (uppercase) letter to check
          TestChar := UpCase(TestString[Index]);

          // Now, let's see if we've seen this letter before.
          CharPos := Pos(TestChar, Letters);

          // If CharPos is zero, either we've seen the letter before, or the
          // character isn't a letter.
          if CharPos > 0 then
            // The letter was seen for the first time, so let's remove it
            // from the string of unseen letters
            Delete(Letters, CharPos, 1);
        end;

      // TestString is a
      Result := Length(Letters) = 0;
    end;
end;

end.

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of acolburn

Nice simple solution. Comments make it easy to understand.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?