Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of Neftali

Neftali's solution

to Phone Number in the Delphi Pascal Track

Published at Jan 17 2020 · 2 comments
Instructions
Test suite
Solution

Clean up user-entered phone numbers so that they can be sent SMS messages.

The North American Numbering Plan (NANP) is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: 1.

NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as area code, followed by a seven-digit local number. The first three digits of the local number represent the exchange code, followed by the unique four-digit number which is the subscriber number.

The format is usually represented as

(NXX)-NXX-XXXX

where N is any digit from 2 through 9 and X is any digit from 0 through 9.

Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.

For example, the inputs

  • +1 (613)-995-0253
  • 613-995-0253
  • 1 613 995 0253
  • 613.995.0253

should all produce the output

6139950253

Note: As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.

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

Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html

Submitting Incomplete Solutions

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

uPhoneNumberTests.pas

//=============================================================================
// You are expected to write an abstraction layer for your class called
// IPhoneNumber.  Your unit 'uPhoneNumber.pas' should only expose the
// abstraction layer along with a function called 'NewPhoneNumber' that returns
// an instance of IPhoneNumber.
//=============================================================================
unit uPhoneNumberTests;

interface
uses
  DUnitX.TestFramework;

const
  CanonicalVersion = '1.7.0';

type

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

    [Test]
    [Ignore]
    procedure Cleans_numbers_with_dots;

    [Test]
    [Ignore]
    procedure Cleans_numbers_with_multiple_spaces;

    [Test]
    [Ignore]
    procedure Invalid_when_9_digits;

    [Test]
    [Ignore]
    procedure Invalid_when_11_digits_does_not_start_with_a_1;

    [Test]
    [Ignore]
    procedure Valid_when_11_digits_and_starting_with_1;

    [Test]
    [Ignore]
    procedure Valid_when_11_digits_and_starting_with_1_even_with_punctuation;

    [Test]
    [Ignore]
    procedure Invalid_when_more_than_11_digits;

    [Test]
    [Ignore]
    procedure Invalid_with_letters;

    [Test]
    [Ignore]
    procedure Invalid_with_punctuations;

    [Test]
    [Ignore]
    procedure Invalid_if_area_code_starts_with_0;

    [Test]
    [Ignore]
    procedure Invalid_if_area_code_starts_with_1;

    [Test]
    [Ignore]
    procedure Invalid_if_exchange_code_starts_with_0;

    [Test]
    [Ignore]
    procedure Invalid_if_exchange_code_starts_with_1;

    [Test]
    [Ignore]
    procedure Invalid_if_area_code_starts_with_0_on_valid_11_digit_number;

    [Test]
    [Ignore]
    procedure Invalid_if_area_code_starts_with_1_on_valid_11_digit_number;

    [Test]
    [Ignore]
    procedure Invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number;

    [Test]
    [Ignore]
    procedure Invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number;

    [Test]
    [Ignore('This is a bonus test')]
    procedure Extract_area_code;

    [Test]
    [Ignore('This is a bonus test')]
    procedure Extract_exchange_code;

    [Test]
    [Ignore('This is a bonus test')]
    procedure Formats_a_number;
  end;

implementation
uses uPhoneNumber;

procedure PhoneNumberTests.Cleans_the_number;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('(223) 456-7890');
  assert.AreEqual('2234567890',phone.Clean);
end;

procedure PhoneNumberTests.Cleans_numbers_with_dots;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('223.456.7890');
  assert.AreEqual('2234567890',phone.Clean);
end;

procedure PhoneNumberTests.Cleans_numbers_with_multiple_spaces;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('223 456   7890   ');
  assert.AreEqual('2234567890',phone.Clean);
end;

procedure PhoneNumberTests.Valid_when_11_digits_and_starting_with_1;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('12234567890');
  assert.AreEqual('2234567890', phone.Clean);
end;

procedure PhoneNumberTests.Valid_when_11_digits_and_starting_with_1_even_with_punctuation;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('+1 (223) 456-7890');
  assert.AreEqual('2234567890', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_when_11_digits_does_not_start_with_a_1;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('22234567890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_when_more_than_11_digits;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('321234567890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_when_9_digits;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('123456789');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_with_letters;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('123-abc-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_with_punctuations;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('123-@:!-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_if_area_code_starts_with_0;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('(023) 456-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_if_area_code_starts_with_0_on_valid_11_digit_number;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('1 (023) 456-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_if_area_code_starts_with_1;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('(123) 456-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_if_area_code_starts_with_1_on_valid_11_digit_number;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('1 (123) 456-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_if_exchange_code_starts_with_0;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('(223) 056-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('1 (223) 056-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_if_exchange_code_starts_with_1;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('(223) 156-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('1 (223) 156-7890');
  assert.AreEqual('', phone.Clean);
end;

procedure PhoneNumberTests.Extract_area_code;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('(223) 456-7890');
  assert.AreEqual('223', phone.Area);
end;

procedure PhoneNumberTests.Extract_exchange_code;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('223.456.7890');
  assert.AreEqual('456', phone.Exchange);
end;

procedure PhoneNumberTests.Formats_a_number;
var phone: IPhoneNumber;
begin
  phone := NewPhoneNumber('2234567890');
  assert.AreEqual('(223) 456-7890', phone.ToString);
end;

initialization
  TDUnitX.RegisterTestFixture(PhoneNumberTests);
end.
unit uPhoneNumber;

interface

type
  IPhoneNumber = interface
    function clean:string;
    function Area:string;
    function Exchange:string;
    function ToString:string;
  end;

  TPhoneNumber = class
  private
    FPhoneNumber: string;
    /// <summary> Only the numbers of the string </summary>
    function OnlyNumbers:string;
    /// <summary>  Las 4 numbers </summary>
    function Number:string;

    property PhoneNumber:string read FPhoneNumber write FPhoneNumber;
  public
    /// <summary> clean correct phone Number or empty </summary>
    function clean:string;
    /// <summary> the first 3 characters </summary>
    function Area:string;
    /// <summary> the 3 characters of the center </summary>
    function Exchange:string;
    function ToString:string; override;
  end;

const
  VALID_NUMBERS = ['0'..'9'];
  VALID_N_DIGITS = ['2'..'9'];
  COUNTRY_CODE = '1';

function NewPhoneNumber(const APhoneNumber:string):TPhoneNumber;

implementation

uses
  System.Sysutils;

function NewPhoneNumber(const APhoneNumber:string):TPhoneNumber;
begin
  Result := TPhoneNumber.Create;
  Result.PhoneNumber := APhoneNumber;
end;

{ IPhoneNumber }

function TPhoneNumber.Area: string;
begin
  Result := clean;
  if (Result <> string.Empty) then
    Result := Copy(Result, 1, 3)
end;

function TPhoneNumber.clean: string;
var
  isCorrect:boolean;
begin
  Result := OnlyNumbers;
  isCorrect := false;

  if (Length(Result) = 10) then begin
    isCorrect := CharInSet(Result[1], VALID_N_DIGITS) and
                 CharInSet(Result[4], VALID_N_DIGITS);
  end
  else if (Length(Result) = 11) then begin
    isCorrect := CharInSet(Result[2], VALID_N_DIGITS) and
                 CharInSet(Result[5], VALID_N_DIGITS) and
                 (Result[1] = COUNTRY_CODE);
    Result := copy(Result, 2, Length(Result));
  end;

  if not iscorrect then
    Result := string.empty;

end;

function TPhoneNumber.Number:string;
begin
  Result := clean;
  if (Result <> string.Empty) then
    Result := Copy(Result, 7, 4)
end;

function TPhoneNumber.Exchange: string;
begin
  Result := clean;
  if (Result <> string.Empty) then
    Result := Copy(Result, 4, 3)
end;

function TPhoneNumber.OnlyNumbers: string;
var
  i:integer;
begin
  Result := string.Empty;
  for i := 0 to (Length(PhoneNumber)) do
    if CharInSet(PhoneNumber[i], VALID_NUMBERS) then
      Result := result + PhoneNumber[i];
end;

function TPhoneNumber.ToString: string;
begin
  Result := clean;
  if (Result <> string.Empty) then     // Correct format (223) 456-7890'
    Result := '(' + Area + ') ' + Exchange + '-' + Number;
end;

end.

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of michkowalczuk
michkowalczuk
commented 264 days ago

Hello, You didn't use interface as it was required. Look at other guys' solutions. Kind regards Michal

Avatar of Neftali

Hi michkowalczuk.
Thanks. You're right.
I hae corrected the definition on the solution.

(edited 262 days ago)

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?