1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-19 22:16:57 +02:00
CodeWars/6kyu/roman_numerals_decoder/solution.cs

36 lines
861 B
C#
Raw Normal View History

using System;
using System.Collections.Generic;
public class RomanDecode {
private static Dictionary<char, int> values = new Dictionary<char, int>() {
{ 'I', 1 },
{ 'V', 5 },
{ 'X', 10 },
{ 'L', 50 },
{ 'C', 100 },
{ 'D', 500 },
{ 'M', 1000 }
};
public static int Solution(string roman) {
int result = 0;
var i = roman.Length - 1;
while (i > 0) {
var val1 = values[roman[i]];
var val2 = values[roman[i - 1]];
if (val1 > val2) {
result += val1 - val2;
i -= 2;
} else {
result += val1;
i--;
}
Console.WriteLine(result);
}
if (i > -1) {
result += values[roman[i]];
}
return result;
}
}