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

# cutlersheridan's solution

## to Roman Numerals in the C# Track

Published at Oct 06 2020 · 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

## 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

### RomanNumeralsTests.cs

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

using Xunit;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000
public static class RomanNumeralExtension
{
public static string ToRoman(this int value)
{
if (value < 1)
{
throw new ArgumentException("The Romans only had integers greater than zero.");
}

string rNums = "";
Dictionary<string, int> numerals = new Dictionary<string, int>{
{"M", 1000},
{"CM", 900},
{"D", 500},
{"CD", 400},
{"C", 100},
{"XC", 90},
{"L", 50},
{"XL", 40},
{"X", 10},
{"IX", 9},
{"V", 5},
{"IV", 4},
{"I", 1}
};
foreach (var num in numerals)
{
while (value >= num.Value)
{
rNums += num.Key;
value -= num.Value;
}
}
return rNums;
}
}``````