🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of jaishreedhage

jaishreedhage'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

See also: http://www.novaroma.org/via_romana/numbers.html

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/RomanNumeralsTest/RomanNumerals.h

//
//  RomanNumerals.h
//  RomanNumerals
//
//  Created by Jaishree Dhage on 12/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface RomanNumerals : NSObject

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

@end

RomanNumerals/RomanNumeralsTest/RomanNumerals.m

//
//  RomanNumerals.m
//  RomanNumerals
//
//  Created by Jaishree Dhage on 12/12/17.
//  Copyright © 2017 Jaishree Dhage. All rights reserved.
//

#import "RomanNumerals.h"

@implementation RomanNumerals

+(NSString *) romanNumeralsForValue:(NSUInteger)value{
    
    NSMutableString *romanString = [NSMutableString string];
    NSInteger times;
    if(value>=1000){
        times = value/1000;
        while(times>0){
            [romanString appendString:@"M"];
            times--;
        }
        value%=1000;
    }
    if(value>=900){
        times = value/900;
        while(times>0){
            [romanString appendString:@"CM"];
            times--;
        }
        value%=900;
    }
    if(value>=500){
        times = value/500;
        while(times>0){
            [romanString appendString:@"D"];
            times--;
        }
        value%=500;
    }
    if(value>=400){
        times = value/400;
        while(times>0){
            [romanString appendString:@"CD"];
            times--;
        }
        value%=400;
    }
    if(value>=100){
        times = value/100;
        while(times>0){
            [romanString appendString:@"C"];
            times--;
        }
        value%=100;
    }
    if(value>=90){
        times = value/90;
        while(times>0){
            [romanString appendString:@"XC"];
            times--;
        }
        value%=90;
    }
    if(value>=50){
        times = value/50;
        while(times>0){
            [romanString appendString:@"L"];
            times--;
        }
        value%=50;
    }
    if(value>=40){
        times = value/40;
        while(times>0){
            [romanString appendString:@"XL"];
            times--;
        }
        value%=40;
    }
    if(value>=10){
        times = value/10;
        while(times>0){
            [romanString appendString:@"X"];
            times--;
        }
        value%=10;
    }
    if(value>=9){
        times = value/9;
        while(times>0){
            [romanString appendString:@"IX"];
            times--;
        }
        value%=9;
    }
    if(value>=5){
        times = value/5;
        while(times>0){
            [romanString appendString:@"V"];
            times--;
        }
        value%=5;
    }
    if(value>=4){
        times = value/4;
        while(times>0){
            [romanString appendString:@"IV"];
            times--;
        }
        value%=4;
    }
    if(value>=1){
        times = value;
        while(times>0){
            [romanString appendString:@"I"];
            times--;
        }
    }
    return romanString;
}

@end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?