1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-19 14:16:55 +02:00
CodeWars/6kyu/roman_numerals_decoder/solution.cs
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

35 lines
861 B
C#

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;
}
}