🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of alevash

alevash's solution

to Isogram in the Delphi Pascal Track

Published at Nov 30 2020 · 0 comments
Instructions
Test suite
Solution

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

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/Isogram

Submitting Incomplete Solutions

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

uIsogramTests.pas

unit uIsogramTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.7.0';

type

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

    [Test]
    [Ignore]
    procedure isogram_with_only_lower_case_characters;

    [Test]
    [Ignore]
    procedure word_with_one_duplicated_character;

    [Test]
    [Ignore]
    procedure word_with_one_duplicated_character_from_the_end_of_the_alphabet;

    [Test]
    [Ignore]
    procedure longest_reported_english_isogram;

    [Test]
    [Ignore]
    procedure word_with_duplicated_character_in_mixed_case;

    [Test]
    [Ignore]
    procedure word_with_duplicated_character_in_mixed_case_lowercase_first;

    [Test]
    [Ignore]
    procedure hypothetical_isogrammic_word_with_hyphen;

    [Test]
    [Ignore]
    procedure hypothetical_word_with_duplicated_character_following_hyphen;

    [Test]
    [Ignore]
    procedure isogram_with_duplicated_hyphen;

    [Test]
    [Ignore]
    procedure made_up_name_that_is_an_isogram;

    [Test]
    [Ignore]
    procedure duplicated_character_in_the_middle;

    [Test]
    [Ignore]
    procedure same_first_and_last_characters;
  end;
	
implementation
uses SysUtils, uIsogram;

{ IsogramTests }

procedure IsogramTests.duplicated_character_in_the_middle;
begin
  Assert.IsFalse(isIsogram('accentor'));
end;

procedure IsogramTests.empty_string;
begin
  Assert.IsTrue(isIsogram(''));
end;

procedure IsogramTests.hypothetical_isogrammic_word_with_hyphen;
begin
  Assert.IsTrue(isIsogram('thumbscrew-japingly'));
end;

procedure IsogramTests.hypothetical_word_with_duplicated_character_following_hyphen;
begin
  Assert.IsFalse(isIsogram('thumbscrew-jappingly'));
end;

procedure IsogramTests.isogram_with_duplicated_hyphen;
begin
  Assert.IsTrue(isIsogram('six-year-old'));
end;

procedure IsogramTests.isogram_with_only_lower_case_characters;
begin
  Assert.IsTrue(isIsogram('isogram'));
end;

procedure IsogramTests.longest_reported_english_isogram;
begin
  Assert.IsTrue(isIsogram('subdermatoglyphic'));
end;

procedure IsogramTests.made_up_name_that_is_an_isogram;
begin
  Assert.IsTrue(isIsogram('Emily Jung Schwartzkopf'));
end;

procedure IsogramTests.same_first_and_last_characters;
begin
  Assert.IsFalse(isIsogram('angola'));
end;

procedure IsogramTests.word_with_duplicated_character_in_mixed_case;
begin
  Assert.IsFalse(isIsogram('Alphabet'));
end;

procedure IsogramTests.word_with_duplicated_character_in_mixed_case_lowercase_first;
begin
  Assert.IsFalse(isIsogram('alphAbet'));
end;

procedure IsogramTests.word_with_one_duplicated_character;
begin
  Assert.IsFalse(isIsogram('eleven'));
end;

procedure IsogramTests.word_with_one_duplicated_character_from_the_end_of_the_alphabet;
begin
  Assert.IsFalse(isIsogram('zzyzx'));
end;

initialization
  TDUnitX.RegisterTestFixture(IsogramTests);
end.
unit uIsogram;

interface

  function isIsogram(s: string): boolean;

implementation

uses strutils, sysutils;

function isIsogram(s: string): boolean;
var i: integer;
  j: Integer;
begin
  if s = '' then exit(true);
  result := true;
  s := lowercase(s);
  s := StringReplace(s, '-', '', [rfReplaceAll]);
  s := StringReplace(s, ' ', '', [rfReplaceAll]);
  for i := 0 to length(s) do
  begin
    for j := i+1 to length(s) do
      if s[i] = s[j] then exit(false)
  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?