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.159 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.158 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.157 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.156 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.155 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.154 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.153 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.152 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.151 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.150 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.149 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.148 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.147 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.146 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.145 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.144 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.143 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.142 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.141 tracks/objective-c/exercises/say/SayExample.m
trackler-2.2.1.140 tracks/objective-c/exercises/say/SayExample.m