-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToIntegerConverter.cs
More file actions
40 lines (37 loc) · 1.17 KB
/
RomanToIntegerConverter.cs
File metadata and controls
40 lines (37 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace CodeChallenge
{
public class RomanToIntegerConverter
{
public int RomanToInt(string s)
{
//Used Dictionary to store the pre defined Values in RomanNumerals
Dictionary<char, int> romanMap = new Dictionary<char, int>()
{
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000},
};
//Used to keep the result
int result = 0;
for (int i = 0; i < s.Length; i++)
{
//Condition to check if the Roman Numral input by user is of Two index or more
if (i < s.Length -1 && romanMap[s[i]] < romanMap[s[i + 1]])
{
//Substract if the first index is greater than the later
result -= romanMap[s[i]];
}
else
{
//Or else Add thenm together
result += romanMap[s[i]];
}
}
return result;
}
}
}