Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of stevenhuey

stevenhuey's solution

to Phone Number in the Objective-C Track

Published at Jul 13 2018 · 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.

Setup

There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/phone-number/PhoneNumber.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.

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.

PhoneNumberTest.m

#import <XCTest/XCTest.h>

#if __has_include("PhoneNumberExample.h")
# import "PhoneNumberExample.h"
# else
# import "PhoneNumber.h"
#endif

NS_ASSUME_NONNULL_BEGIN


@interface PhoneNumberTest : XCTestCase

@end

@implementation PhoneNumberTest

- (void)testCleansNumber {
  NSString *startingNumber = @"(123) 456-7890";
  NSString *expected = @"1234567890";
  PhoneNumber *number = [[PhoneNumber alloc] initWithString:startingNumber];
  NSString *result = [number number];
  XCTAssertEqualObjects(result, expected);
}

- (void)testCleansNumberWithDots {
  NSString *startingNumber = @"123.456.7890";
  NSString *expected = @"1234567890";
  PhoneNumber *number = [[PhoneNumber alloc] initWithString:startingNumber];
  NSString *result = [number number];
  XCTAssertEqualObjects(result, expected);
}

- (void)testValidWithElevenDigitsAndFirstIsOne {
  NSString *startingNumber = @"11234567890";
  NSString *expected = @"1234567890";
  PhoneNumber *number = [[PhoneNumber alloc] initWithString:startingNumber];
  NSString *result = [number number];
  XCTAssertEqualObjects(result, expected);
}

- (void)testInvalidWhenElevenDigits {
  NSString *startingNumber = @"21234567890";
  NSString *expected = @"0000000000";
  PhoneNumber *number = [[PhoneNumber alloc] initWithString:startingNumber];
  NSString *result = [number number];
  XCTAssertEqualObjects(result, expected);
}

- (void)testInvalidWhenNineDigits {
  NSString *startingNumber = @"123456789";
  NSString *expected = @"0000000000";
  PhoneNumber *number = [[PhoneNumber alloc] initWithString:startingNumber];
  NSString *result = [number number];
  XCTAssertEqualObjects(result, expected);
}

- (void)testAreaCode {
  NSString *startingNumber = @"1234567890";
  NSString *expected = @"123";
  PhoneNumber *number = [[PhoneNumber alloc] initWithString:startingNumber];
  NSString *result = [number areaCode];
  XCTAssertEqualObjects(result, expected);
}

- (void)testPrettyPrint {
  NSString *startingNumber = @"1234567890";
  NSString *expected = @"(123) 456-7890";
  PhoneNumber *number = [[PhoneNumber alloc] initWithString:startingNumber];
  NSString *result = [number description];
  XCTAssertEqualObjects(result, expected);
}

- (void)testPrettyPrintWithFullUSPhoneNumber {
  NSString *startingNumber = @"11234567890";
  NSString *expected = @"(123) 456-7890";
  PhoneNumber *number = [[PhoneNumber alloc] initWithString:startingNumber];
  NSString *result = [number description];
  XCTAssertEqualObjects(result, expected);
}

@end
NS_ASSUME_NONNULL_END
//
//  PhoneNumber.m
//  Phone Number
//
//  Created by Steven Huey on 5/6/16.
//  Copyright © 2016 Steve Huey. All rights reserved.
//

#import "PhoneNumber.h"

@interface PhoneNumber ()
@property (copy) NSString* numberString;
@property (assign) BOOL numberIsValidated;

- (NSString*)sanitizeNumber:(NSString*)number;
- (BOOL)isValidNumber:(NSString*)number;
- (NSString*)firstThree;
- (NSString*)lastFour;
@end

@implementation PhoneNumber
- (instancetype)initWithString:(NSString*)number
{
   self = [super init];
   
   if (self)
   {
      self.numberString = number;
      self.numberIsValidated = NO;
   }
   
   return self;
}

- (NSString*)description
{
   return [NSString stringWithFormat:@"(%@) %@-%@",
           [self areaCode], [self firstThree], [self lastFour]];
}

- (NSString*)number
{
   if (!self.numberIsValidated)
   {
      NSString* number = [self sanitizeNumber:self.numberString];
      
      if ([self isValidNumber:number])
      {
         self.numberString = number;
      }
      else
      {
         self.numberString = @"0000000000";
      }
      
      self.numberIsValidated = YES;
   }
   
   return self.numberString;
}

- (NSString*)areaCode
{
   return [[self number] substringWithRange:NSMakeRange(0, 3)];
}

- (NSString*)sanitizeNumber:(NSString*)number
{
   NSCharacterSet* invalidCharacters = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
   NSString* value = [[number componentsSeparatedByCharactersInSet:invalidCharacters]
                      componentsJoinedByString:@""];
   
   // Strip any leading 1s if the number is 11 digits
   if ((11 == [value length]) && [value hasPrefix:@"1"])
   {
      value = [value substringFromIndex:1];
   }
   
   return value;
}

- (BOOL)isValidNumber:(NSString*)number
{
   return (10 == [number length]);
}

- (NSString*)firstThree
{
   return [self.numberString substringWithRange:NSMakeRange(3, 3)];
}

- (NSString*)lastFour
{
   return [self.numberString substringWithRange:NSMakeRange(6, 4)];
}
@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?