Avatar of artemkorsakov

artemkorsakov's solution

to Roman Numerals in the C# Track

Published at Feb 27 2019 · 0 comments
Instructions
Test suite
Solution

Write a method 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

Hints

This exercise requires you to write an extension method. For more information, see this page.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit RomanNumerals.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

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.cs

// This file was auto-generated based on version 1.2.0 of the canonical data.

using Xunit;

public class RomanNumeralsTest
{
    [Fact]
    public void Number_1_is_a_single_i()
    {
        Assert.Equal("I", 1.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_2_is_two_is()
    {
        Assert.Equal("II", 2.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_3_is_three_is()
    {
        Assert.Equal("III", 3.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_4_being_5_1_is_iv()
    {
        Assert.Equal("IV", 4.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_5_is_a_single_v()
    {
        Assert.Equal("V", 5.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_6_being_5_1_is_vi()
    {
        Assert.Equal("VI", 6.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_9_being_10_1_is_ix()
    {
        Assert.Equal("IX", 9.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_20_is_two_xs()
    {
        Assert.Equal("XXVII", 27.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_48_is_not_50_2_but_rather_40_8()
    {
        Assert.Equal("XLVIII", 48.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_49_is_not_40_5_4_but_rather_50_10_10_1()
    {
        Assert.Equal("XLIX", 49.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_50_is_a_single_l()
    {
        Assert.Equal("LIX", 59.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_90_being_100_10_is_xc()
    {
        Assert.Equal("XCIII", 93.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_100_is_a_single_c()
    {
        Assert.Equal("CXLI", 141.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_60_being_50_10_is_lx()
    {
        Assert.Equal("CLXIII", 163.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_400_being_500_100_is_cd()
    {
        Assert.Equal("CDII", 402.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_500_is_a_single_d()
    {
        Assert.Equal("DLXXV", 575.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_900_being_1000_100_is_cm()
    {
        Assert.Equal("CMXI", 911.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_1000_is_a_single_m()
    {
        Assert.Equal("MXXIV", 1024.ToRoman());
    }

    [Fact(Skip = "Remove to run test")]
    public void Number_3000_is_three_ms()
    {
        Assert.Equal("MMM", 3000.ToRoman());
    }
}
using System;
using System.Linq;

public static class RomanNumeralExtension
{
    private enum RomanLetter { I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 }
    private static int _number;

    public static string ToRoman(this int value)
    {
        _number = value;
        return Enum.GetValues(typeof(RomanLetter)).Cast<RomanLetter>().Reverse()
            .Aggregate("", (current, romanLetter) => current + GetRomanLetter(romanLetter));
    }

    private static string GetRomanLetter(RomanLetter romanLetter)
    {
        if (_number == 0)
        {
            return "";
        }

        var result = "";
        var count = _number / (int)romanLetter;
        if (count > 0)
        {
            result += new string(romanLetter.ToChar(), count);
            _number = _number - count * (int)romanLetter;
        }
        var diff = _number - ((int)romanLetter - romanLetter.GetPreviousLetter());
        if (diff < 0)
        {
            return result;
        }
        result += Enum.GetName(typeof(RomanLetter), romanLetter.GetPreviousLetter()) + romanLetter.ToChar();
        _number = diff;
        return result;
    }

    private static char ToChar(this RomanLetter rl)
    {
        return Enum.GetName(typeof(RomanLetter), rl)[0];
    }

    private static int GetPreviousLetter(this RomanLetter rl)
    {
        return (int)rl >= 500 ? 100 : (int)rl >= 50 ? 10 : (int)rl >= 5 ? 1 : 0;
    }
}

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?