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

dynamite8'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 Jenny Chang Ho on 2015-10-18.
//  Copyright © 2015 Exercism Test. All rights reserved.
//

#import "PhoneNumber.h"

static NSString *const DEFAULT_NUMBER = @"0000000000";

@interface PhoneNumber ()

- (NSString*)filterInvalidChars:(NSString*)number;

@end

@implementation PhoneNumber

- (instancetype)initWithString:(NSString*)phoneNumber {
    
    if (self == nil) {
        self = [super init];
    }
    
    self.phoneNumber = [self filterInvalidChars:phoneNumber];
    
    return self;
}

- (NSString*)number {
    
    return self.phoneNumber;
}

- (void)phoneNumber:(NSString*)number {
    
    _phoneNumber = number;
}

- (NSString*)areaCode {
    
    return [self.phoneNumber substringToIndex:3];
}

- (NSString*)description {
    
    return [NSString stringWithFormat:@"(%@) %@-%@", [self.phoneNumber substringWithRange:NSMakeRange(0,3)], [self.phoneNumber substringWithRange:NSMakeRange(3,3)], [self.phoneNumber substringWithRange:NSMakeRange(6,4)]];
}

- (NSString*)filterInvalidChars:(NSString*)number {
    
    NSCharacterSet *notAllowedChars = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    NSString *cleanNumber = [[number componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:@""];
    
    if (cleanNumber.length != 10) {
        if(cleanNumber.length == 11 && [[cleanNumber substringToIndex:1] isEqualToString:@"1"]) {
            cleanNumber = [cleanNumber substringFromIndex:1];
        } else {
            cleanNumber = DEFAULT_NUMBER;
        }
    }
    
    return cleanNumber;
}

@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?