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

michkowalczuk's solution

to Phone Number in the Delphi Pascal Track

Published at Nov 13 2020 · 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 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
  /// <summary>
  ///   Abstraction layer for a class that cleans up differently formatted 
  ///   NANP (The North American Numbering Plan) telephone numbers by removing 
  ///   punctuation and the country code (1), if present.
  /// </summary>
  IPhoneNumber = interface
    /// <returns>
    ///   cleaned telephone number
    /// </returns>
    function Clean: String;

    /// <returns>
    ///   area code
    /// </returns>
    function Area: String;

    /// <returns>
    ///   exchange code
    /// </returns>
    function Exchange: String;

    /// <returns>
    ///   telephone number formatted as below
    ///   "(area_code) exchange_code-subscriber_code" 
    /// </returns>
    function ToString: String;
  end;

/// <summary>
///   Function that returns an instance of IPhoneNumber.  
/// </summary>
function NewPhoneNumber(const phone_number: String): IPhoneNumber;

implementation
uses
  System.Character,
  System.SysUtils;

{$REGION 'TPhoneNumber'}
type
  TPhoneNumber = class(TInterfacedObject, IPhoneNumber)
    private
      FClean: String;
      FArea: String;
      FExchange: String;
      FToString: String;
      FSubscriber: String;
    public
      constructor Create(const phone_number: String);
      function Clean: String;
      function Area: String;
      function Exchange: String;
      function ToString: String; override;
  end;

constructor TPhoneNumber.Create(const phone_number: String);
var
  c: Char;
  builder: TStringBuilder;
  len: Integer;
begin
  builder := TStringBuilder.Create;
  try
    for c in phone_number do
      if c.IsDigit then
        builder.Append(c);
        
    len := builder.Length;
    if (len < 10) or (len > 11) then begin
      builder.Clear;
      Exit;
    end
    // removing the country code (1) if present.
    else if (len = 11) then begin
      if (builder.Chars[0] = '1') then
        builder.Remove(0, 1)
      else begin
        builder.Clear;
        Exit;
      end;
    end;

    // 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.
    if (builder.Chars[0] <= '1') or (builder.Chars[3] <= '1') then
      builder.Clear;
  finally
    FClean := builder.ToString;
    if FClean <> '' then begin
      // first 3 digits
      FArea := builder.Chars[0] + builder.Chars[1] + builder.Chars[2];

      // 3 digits in the middle
      FExchange := builder.Chars[3] + builder.Chars[4] + builder.Chars[5];

      // last 4 digits
      FSubscriber := builder.Chars[6] + builder.Chars[7] + builder.Chars[8] + builder.Chars[9];
      
      FToString := Format('(%s) %s-%s',[FArea, FExchange, FSubscriber]);
    end;
    builder.Free;
  end;
end;

function TPhoneNumber.Clean: String;
begin
  Result := FClean;
end;

function TPhoneNumber.Area: String;
begin
  Result := FArea;
end;

function TPhoneNumber.Exchange: String;
begin
  Result := FExchange;
end;

function TPhoneNumber.ToString: String;
begin
  Result := FToString;
end;
{$ENDREGION}

function NewPhoneNumber(const phone_number: String): IPhoneNumber;
begin
  Result := TPhoneNumber.Create(phone_number);
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?