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

robtimp'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
//  PhoneNumber
//
//  Created by Rob Hudson on 12/31/15.
//  Copyright © 2015 Rob Hudson. All rights reserved.
//

#import "PhoneNumber.h"

@implementation PhoneNumber {
    NSString *_number;
}

- (id)initWithString:(NSString *)string {
    self = super.init;
    
    if (self) {
        NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
        NSString *number = [[string componentsSeparatedByCharactersInSet:set] componentsJoinedByString:@""];
        
        if (number.length == 10) {
            _number = number;
        } else if (number.length == 11 && [number hasPrefix:@"1"]) {
            _number = [number substringFromIndex:1];
        } else {
            _number = [self invalidNumber];
        }
    }
    
    return self;
}

- (NSString *)number {
    return _number;
}

- (NSString *)areaCode {
    return [_number substringToIndex:3];
}

- (NSString *)invalidNumber {
    return @"0000000000";
}

- (NSString *)description {
    NSString *areaCode = [self areaCode];
    NSString *middleThree = [_number substringWithRange:NSMakeRange(3, 3)];
    NSString *lastFour = [_number substringFromIndex:6];
    
    return [NSString stringWithFormat:@"(%@) %@-%@", areaCode, middleThree, lastFour];
}

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