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.139 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.138 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.137 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.136 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.135 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.134 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.133 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.132 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.131 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.130 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.129 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.128 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.127 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.126 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.125 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.124 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.123 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.122 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.121 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.120 tracks/objective-c/exercises/say/SayExample.m