Sha256: f6a9eb5e63df48fae7d5d5b8fdd6c253fc4488624fc6b5bbf13fc5647af3fdfb
Contents?: true
Size: 1.3 KB
Versions: 396
Compression:
Stored size: 1.3 KB
Contents
#import "RomanNumeralsExample.h" static NSDictionary<NSNumber *, NSString *>* arabicToRoman; @implementation RomanNumerals + (void)initialize { if (self == [RomanNumerals class]) { arabicToRoman = @{ @1000 : @"M", @900 : @"CM", @500 : @"D", @400 : @"CD", @100 : @"C", @90 : @"XC", @50 : @"L", @40 : @"XL", @10 : @"X", @9 : @"IX", @5 : @"V", @4 : @"IV", @1 : @"I" }; } } + (NSString *)romanNumeralsForValue:(int)value { NSMutableString *result = [[NSMutableString alloc] initWithString:@""]; NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO]; NSArray<NSNumber *> *sortedKeys = [[arabicToRoman allKeys] sortedArrayUsingDescriptors:@[highestToLowest]]; for (NSNumber *arabic in sortedKeys) { while (value >= [arabic intValue]) { [result appendString:arabicToRoman[arabic]]; value -= [arabic intValue]; } } return result; } @end
Version data entries
396 entries across 396 versions & 1 rubygems