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

Greycoat21's solution

to Phone Number in the Delphi Pascal Track

Published at Mar 31 2019 · 0 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 receive assistance 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 Exchange: string;
    function Area: string;
    function ToString: string;
    function Clean: string;
  end;

function NewPhoneNumber(NewNumber: string): IPhoneNumber;

implementation

uses
  SysUtils;

type
  // Phone numbers in the format: '1 (NXX)-NXX-XXXX'
  // N in [2..9], X in [0..9]
  TPhoneNumber = Class(TInterfacedObject, IPhoneNumber)
  private
    FRawNumber: string;
  public
    constructor Create(NewNumber: string);

    function Clean: string; // 'NXXNXXXXXX'
    function ToString: string; // '1 (NXX)-NXX-XXXX'

    function Country: string;    // '1'
    function Area: string;       // 'NXX'
    function Exchange: string;   // 'NXX'
    function Subscriber: string; // 'XXXX'
  const
    CountryCode = '1'; // -> USA
    XDigits = ['0' .. '9'];
    NDigits = ['2' .. '9'];
    NDigitIndices = [1, 4];
    CountryCodeIndex = 1;
    ValidNumberLength = 10; // Excluding country code
  end;

function NewPhoneNumber(NewNumber: string): IPhoneNumber;
begin
  Result := TPhoneNumber.Create(NewNumber);
end;

{ TPhoneNumber }

constructor TPhoneNumber.Create(NewNumber: string);
begin
  FRawNumber := NewNumber;
end;

function TPhoneNumber.Clean: string;

  function HasValidNDigits(Number: string): Boolean;
  var
    Index: Integer;
  begin
    // Default Result to True
    Result := True;

    // Check that each N Digit Index contains N Digits only (2-9).
    for Index := Low(Number) to High(Number) do
      if (Index in NDigitIndices)
      and not CharInSet(Number[Index], NDigits) then
        Result := False
  end;

var
  Character: Char;
  CleanNumber: string;
begin
  // Initialize Result
  Result := '';

  // Transcribe all valid digits from RawNumber to Result
  // (i.e. remove all non-digit characters).
  for Character in FRawNumber do
    if CharInSet(Character, XDigits) then
      Result := Result + Character;

  // Remove CountryCode if present
  if Result[1] = CountryCode then
    Result := Result.Substring(1);


  // Validate Clean Number
  if (Result.Length <> ValidNumberLength) then
    Exit('')
  else if not HasValidNDigits(Result) then
    Exit('')
end;

function TPhoneNumber.Country: string;
begin
  Result := CountryCode;
end;

function TPhoneNumber.Area: string;
const
  AreaIndex = 0;
  AreaLength = 3;
begin
  Result := Clean.Substring(AreaIndex, AreaLength);
end;

function TPhoneNumber.Exchange: string;
const
  ExchangeIndex = 3;
  ExchangeLength = 3;
begin
  Result := Clean.Substring(ExchangeIndex, ExchangeLength);
end;

function TPhoneNumber.Subscriber: string;
const
  SubscriberIndex = 6;
  SubscriberLength = 4;
begin
  Result := Clean.Substring(SubscriberIndex, SubscriberLength)
end;

function TPhoneNumber.ToString: string;
begin
  Result := '(' + Area + ') ' + Exchange + '-' + Subscriber;
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?