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

Whookie's solution

to Pangram in the Delphi Pascal Track

Published at Feb 06 2020 · 0 comments
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

Uses
  System.Classes, System.SysUtils;

function isPangram(const ASentence: String): Boolean;

implementation

Const
  CPAN_COUNT = 26;

function isPangram(const ASentence: String): Boolean;
var
  LStr: String;
  LNeeded: Array[' '..#127] Of Boolean;
  LSum: Integer;
  i: Integer;
begin
  FillChar(LNeeded, SizeOf(LNeeded), FALSE);
  FillChar(LNeeded['a'], CPAN_COUNT, TRUE);

  LSum := 0;
  LStr := LowerCase(ASentence);
  Result := FALSE;
  for i := 1 to Length(LStr) do
  begin
    if LNeeded[LStr[i]] then
    begin
      LNeeded[LStr[i]] := FALSE;
      Inc(LSum);
      if LSum = CPAN_COUNT then
      begin
        Result := TRUE;
        Break;
      end;
    end;
  end;
end;

end.

Community comments

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

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?