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

KiligWY's solution

to Roman Numerals in the Objective-C Track

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

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

`````` 1  => I
10  => X
7  => VII
``````

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

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/roman-numerals/RomanNumerals.m
``````

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

Source

The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

Submitting Incomplete Solutions

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

RomanNumeralsTest.m

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

#if __has_include("RomanNumeralsExample.h")
# import "RomanNumeralsExample.h"
# else
# import "RomanNumerals.h"
#endif

@interface RomanNumeralsTest : XCTestCase

@end

@implementation RomanNumeralsTest

- (void)test1 {
XCTAssertEqualObjects(@"I", [RomanNumerals romanNumeralsForValue:1]);
}

- (void)test2 {
XCTAssertEqualObjects(@"II", [RomanNumerals romanNumeralsForValue:2]);
}

- (void)test3 {
XCTAssertEqualObjects(@"III", [RomanNumerals romanNumeralsForValue:3]);
}

- (void)test4 {
XCTAssertEqualObjects(@"IV", [RomanNumerals romanNumeralsForValue:4]);
}

- (void)test5 {
XCTAssertEqualObjects(@"V", [RomanNumerals romanNumeralsForValue:5]);
}

- (void)test6 {
XCTAssertEqualObjects(@"VI", [RomanNumerals romanNumeralsForValue:6]);
}

- (void)test9 {
XCTAssertEqualObjects(@"IX", [RomanNumerals romanNumeralsForValue:9]);
}

- (void)test27 {
XCTAssertEqualObjects(@"XXVII", [RomanNumerals romanNumeralsForValue:27]);
}

- (void)test48 {
XCTAssertEqualObjects(@"XLVIII", [RomanNumerals romanNumeralsForValue:48]);
}

- (void)test59 {
XCTAssertEqualObjects(@"LIX", [RomanNumerals romanNumeralsForValue:59]);
}

- (void)test93 {
XCTAssertEqualObjects(@"XCIII", [RomanNumerals romanNumeralsForValue:93]);
}

- (void)test141 {
XCTAssertEqualObjects(@"CXLI", [RomanNumerals romanNumeralsForValue:141]);
}

- (void)test163 {
XCTAssertEqualObjects(@"CLXIII", [RomanNumerals romanNumeralsForValue:163]);
}

- (void)test402 {
XCTAssertEqualObjects(@"CDII", [RomanNumerals romanNumeralsForValue:402]);
}

- (void)test575 {
XCTAssertEqualObjects(@"DLXXV", [RomanNumerals romanNumeralsForValue:575]);
}

- (void)test911 {
XCTAssertEqualObjects(@"CMXI", [RomanNumerals romanNumeralsForValue:911]);
}

- (void)test1024 {
XCTAssertEqualObjects(@"MXXIV", [RomanNumerals romanNumeralsForValue:1024]);
}

- (void)test3000 {
XCTAssertEqualObjects(@"MMM", [RomanNumerals romanNumeralsForValue:3000]);
}

@end``````

RomanNumerals.h

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

@interface RomanNumerals : NSObject

+ (NSString *)romanNumeralsForValue:(NSInteger)value;

@end``````

RomanNumerals.m

``````#import "RomanNumerals.h"

@implementation RomanNumerals

+ (NSString *)romanNumeralsForValue:(NSInteger)value {
if (value < 1 || value >= 4000) {
return @"";
}
NSMutableString *string = [NSMutableString string];
NSArray *array = @[ @"M", @1000,
@"D", @500,
@"C", @100,
@"L", @50,
@"X", @10,
@"V", @5,
@"I", @1 ];
NSInteger index = 0;
while (value > 0) {
NSInteger indexValue = [array[index + 1] integerValue];
if (value / indexValue > 0) {
if (value / indexValue == 4) {
[string appendFormat:@"%@%@", array[index], array[index - 2]];
value -= (value / indexValue * indexValue);
} else if (index + 3 < array.count && value / [array[index + 3] integerValue] == 9) {
[string appendFormat:@"%@%@", array[index + 2], array[index - 2]];
value -= (value / [array[index + 3] integerValue] * [array[index + 3] integerValue]);
} else {
for (NSInteger i = 0; i < value / indexValue; ++i) {
[string appendString:array[index]];
}
value -= (value / indexValue * indexValue);
}
}
index += 2;
}
return string;
}

@end``````