ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

KiligWY's solution

to Say in the Objective-C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Given a number from 0 to 999,999,999,999, spell out that number in English.

Step 1

Handle the basic case of 0 through 99.

If the input to the program is `22`, then the output should be `'twenty-two'`.

Your program should complain loudly if given a number outside the blessed range.

Some good test cases for this program are:

• 0
• 14
• 50
• 98
• -1
• 100

Extension

If you're on a Mac, shell out to Mac OS X's `say` program to talk out loud.

Step 2

Implement breaking a number up into chunks of thousands.

So `1234567890` should yield a list like 1, 234, 567, and 890, while the far simpler `1000` should yield just 1 and 0.

The program must also report any values that are out of range.

Step 3

Now handle inserting the appropriate scale word between those chunks.

So `1234567890` should yield `'1 billion 234 million 567 thousand 890'`

The program must also report any values that are out of range. It's fine to stop at "trillion".

Step 4

Put it all together to get nothing but plain English.

`12345` should give `twelve thousand three hundred forty-five`.

The program must also report any values that are out of range.

Extensions

Use and (correctly) when spelling out the number in English:

• 14 becomes "fourteen".
• 100 becomes "one hundred".
• 120 becomes "one hundred and twenty".
• 1002 becomes "one thousand and two".
• 1323 becomes "one thousand three hundred and twenty-three".

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/say/Say.m
``````

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

Source

A variation on JavaRanch CattleDrive, exercise 4a http://www.javaranch.com/say.jsp

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

SayTest.m

``````#import <XCTest/XCTest.h>

#if __has_include("SayExample.h")
# import "SayExample.h"
#else
# import "Say.h"
#endif

@interface SayTest : XCTestCase

@end

@implementation SayTest

- (void)testZero {
XCTAssertEqualObjects(@"zero", [Say say:0]);
}

- (void)testOne {
XCTAssertEqualObjects(@"one", [Say say:1]);
}

- (void)testFourteen {
XCTAssertEqualObjects(@"fourteen", [Say say:14]);
}

- (void)testTwenty {
XCTAssertEqualObjects(@"twenty", [Say say:20]);
}

- (void)testTwentyTwo {
XCTAssertEqualObjects(@"twenty-two", [Say say:22]);
}

- (void)testOneHundred {
XCTAssertEqualObjects(@"one hundred", [Say say:100]);
}

- (void)testOneHundredTwentyThree {
XCTAssertEqualObjects(@"one hundred twenty-three", [Say say:123]);
}

- (void)testOneThousand {
XCTAssertEqualObjects(@"one thousand", [Say say:1000]);
}

- (void)testOneThousandTwoHundredThirtyFour {
XCTAssertEqualObjects(@"one thousand two hundred thirty-four", [Say say:1234]);
}

- (void)testOneMillion {
XCTAssertEqualObjects(@"one million", [Say say:1000000]);
}

- (void)testOneMillionTwoThousandThreeHundredFortyFive {
XCTAssertEqualObjects(@"one million two thousand three hundred forty-five", [Say say:1002345]);
}

- (void)testOneBillion {
XCTAssertEqualObjects(@"one billion", [Say say:1000000000]);
}

- (void)testABigNumber {
XCTAssertEqualObjects(@"nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three", [Say say:987654321123]);
}

- (void)testNumbersBelowZeroAreOutOfRange {
XCTAssertNil([Say say:-1]);
}

- (void)testNumbersAbove999999999999AreOutOfRange {
XCTAssertNil([Say say:1000000000000]);
}

@end``````

Say.h

``````#import <Foundation/Foundation.h>

@interface Say : NSObject

+ (NSString *)say:(long)number;

@end``````

Say.m

``````#import "Say.h"

@implementation Say

+ (NSString *)say:(long)number {
if (number < 0 || number >= 1000000000000) {
return nil;
}
if (number == 0) {
return @"zero";
}
NSMutableString *string = [NSMutableString string];
if (number / 1000000000 > 0) {
[string appendFormat:@"%@ billion", [self sayLessThanOneThousand:number / 1000000000]];
number -= (number / 1000000000 * 1000000000);
}
if (number / 1000000 > 0) {
[string appendFormat:@" %@ million", [self sayLessThanOneThousand:number / 1000000]];
number -= (number / 1000000 * 1000000);
}
if (number / 1000 > 0) {
[string appendFormat:@" %@ thousand", [self sayLessThanOneThousand:number / 1000]];
number -= (number / 1000 * 1000);
}
[string appendFormat:@" %@", [self sayLessThanOneThousand:number]];
[string replaceOccurrencesOfString:@"  " withString:@" " options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];
return [string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
}

+ (NSString *)sayLessThanOneThousand:(NSInteger)number {
NSMutableString *string = [NSMutableString string];
if (number > 99) {
[string appendFormat:@" %@ hundred", dict()[@(number / 100)]];
number -= (number / 100 * 100);
}
if (number > 19) {
[string appendFormat:@" %@", dict()[@(number / 10 * 10)]];
if (number % 10 > 0) {
[string appendFormat:@"-%@", dict()[@(number % 10)]];
}
} else {
if (number != 0) {
[string appendFormat:@" %@", dict()[@(number)]];
}
}
return string;
}

NSDictionary *dict(void) {
return @{
@1 : @"one",
@2 : @"two",
@3 : @"three",
@4 : @"four",
@5 : @"five",
@6 : @"six",
@7 : @"seven",
@8 : @"eight",
@9 : @"nine",
@10 : @"ten",
@11 : @"eleven",
@12 : @"twelve",
@13 : @"thirteen",
@14 : @"fourteen",
@15 : @"fifteen",
@16 : @"sixteen",
@17 : @"seventeen",
@18 : @"eighteen",
@19 : @"nineteen",
@20 : @"twenty",
@30 : @"thirty",
@40 : @"forty",
@50 : @"fifty",
@60 : @"sixty",
@70 : @"seventy",
@80 : @"eighty",
@90 : @"ninety",
};
}

@end``````

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?