Sha256: bfdabfa973220eb6b371ca971cd00f3a9ee3596cdefb0d0bfc9494c096387c6d

Contents?: true

Size: 1.97 KB

Versions: 111

Compression:

Stored size: 1.97 KB

Contents

#import "SayExample.h"

@implementation Say

+ (NSString *)say:(long)number {
    NSArray<NSString *> *smallNumbers = @[
                                          @"zero", @"one", @"two", @"three", @"four", @"five",
                                          @"six", @"seven", @"eight", @"nine", @"ten",
                                          @"eleven", @"twelve", @"thirteen", @"fourteen", @"fifteen",
                                          @"sixteen", @"seventeen", @"eighteen", @"nineteen"
                                          ];
    
    NSArray<NSString *> *decades = @[
                                     @"twenty", @"thirty", @"forty", @"fifty",
                                     @"sixty", @"seventy", @"eighty", @"ninety"
                                     ];
    
    NSArray<NSString *> *largeGroupNames = @[@"billion", @"million", @"thousand", @"hundred"];
    
    NSArray<NSNumber *> *largeGroupAmounts = @[@1000000000, @1000000, @1000, @100];
    
    if (number < 0 || number >= 1000000000000) {
        return NULL;
    }

    if (number < 20) {
        return smallNumbers[number];
    }
    
    for (int i = 0; i < largeGroupAmounts.count; i++) {
        int amount = largeGroupAmounts[i].intValue;
        
        if (number >= amount) {
            NSString *groupName = largeGroupNames[i];
            NSString *result = [[Say say:(number / amount)] stringByAppendingString:[NSString stringWithFormat:@" %@", groupName]];
            long remainder = number % amount;
            
            if (remainder == 0) {
                return result;
            }
            return [result stringByAppendingString:[NSString stringWithFormat:@" %@", [Say say:remainder]]];
        }
    }
    
    long decade = number / 10;
    int remainder = number % 10;
    NSString *decadeName = decades[decade - 2];
    
    if (remainder == 0) {
        return decadeName;
    }
    return [decadeName stringByAppendingString:[NSString stringWithFormat:@"-%@", [Say say:remainder]]];
}

@end

Version data entries

111 entries across 111 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.77 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.76 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.75 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.74 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.73 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.72 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.71 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.70 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.69 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.68 tracks/objective-c/exercises/say/SayExample.m