Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Phone Number in the Perl 6 Track

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.

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove . --exec=perl6 in the exercise directory. You can also add the -v flag e.g. prove . --exec=perl6 -v to display all tests, including any optional tests marked as 'TODO'.

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 can see how others have completed the exercise.

phone-number.t

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use Phone;
plan 14;

my Version:D $version = v5;

if Phone.^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\nPhone is {Phone.^ver.gist}. "
    ~ "Test is {$version.gist}.\n";
}

my $c-data = from-json $=pod.pop.contents;
for @($c-data<cases>[0]<cases>) {
  if .<expected> {
    is clean-number(.<input><phrase>), |.<expected description>;
  } else {
    nok clean-number(.<input><phrase>), .<description>;
  }
}

=head2 Canonical Data
=begin code
{
  "exercise": "phone-number",
  "version": "1.4.0",
  "cases": [
    {
      "description": "Cleanup user-entered phone numbers",
      "comments": [
        " Returns the cleaned phone number if given number is valid, "
      , " else returns nil. Note that number is not formatted,       "
      , " just a 10-digit number is returned.                        "
      ],
      "cases": [
        {
          "description": "cleans the number",
          "property": "clean",
          "input": {
            "phrase": "(223) 456-7890"
          },
          "expected": "2234567890"
        },
        {
          "description": "cleans numbers with dots",
          "property": "clean",
          "input": {
            "phrase": "223.456.7890"
          },
          "expected": "2234567890"
        },
        {
          "description": "cleans numbers with multiple spaces",
          "property": "clean",
          "input": {
            "phrase": "223 456   7890   "
          },
          "expected": "2234567890"
        },
        {
          "description": "invalid when 9 digits",
          "property": "clean",
          "input": {
            "phrase": "123456789"
          },
          "expected": null
        },
        {
          "description": "invalid when 11 digits does not start with a 1",
          "property": "clean",
          "input": {
            "phrase": "22234567890"
          },
          "expected": null
        },
        {
          "description": "valid when 11 digits and starting with 1",
          "property": "clean",
          "input": {
            "phrase": "12234567890"
          },
          "expected": "2234567890"
        },
        {
          "description": "valid when 11 digits and starting with 1 even with punctuation",
          "property": "clean",
          "input": {
            "phrase": "+1 (223) 456-7890"
          },
          "expected": "2234567890"
        },
        {
          "description": "invalid when more than 11 digits",
          "property": "clean",
          "input": {
            "phrase": "321234567890"
          },
          "expected": null
        },
        {
          "description": "invalid with letters",
          "property": "clean",
          "input": {
            "phrase": "123-abc-7890"
          },
          "expected": null
        },
        {
          "description": "invalid with punctuations",
          "property": "clean",
          "input": {
            "phrase": "123-@:!-7890"
          },
          "expected": null
        },
        {
          "description": "invalid if area code starts with 0",
          "property": "clean",
          "input": {
            "phrase": "(023) 456-7890"
          },
          "expected": null
        },
        {
          "description": "invalid if area code starts with 1",
          "property": "clean",
          "input": {
            "phrase": "(123) 456-7890"
          },
          "expected": null
        },
        {
          "description": "invalid if exchange code starts with 0",
          "property": "clean",
          "input": {
            "phrase": "(223) 056-7890"
          },
          "expected": null
        },
        {
          "description": "invalid if exchange code starts with 1",
          "property": "clean",
          "input": {
            "phrase": "(223) 156-7890"
          },
          "expected": null
        }
      ]
    }
  ]
}
=end code
unit module Phone;

class Phone is export {
    has Str $.number;
    has Str $.area-code;
    has Str $.pretty;

    submethod BUILD(Str(Cool):$!number) {
        $!number ~~ s:g/\D//;
        if $!number.chars == 11 {
            die "Only north american numbers allowed" if $!number.substr(0, 1) != '1';
            $!number .= substr(1);
        }
        if $!number.chars != 10 {
            die 'Must have 10 digits';
        }
        $!area-code = $!number.substr(0, 3);
        $!pretty = sprintf('(%s) %s-%s',
                           $!area-code,
                           $!number.substr(3, 3),
                           $!number.substr(6));
    }
}

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?