Skip to content

Commit 493b2dc

Browse files
Create Roman Numbers to Decimals
1 parent 9afb75d commit 493b2dc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Roman Numbers to Decimals

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tallies = {
2+
'I': 1,
3+
'V': 5,
4+
'X': 10,
5+
'L': 50,
6+
'C': 100,
7+
'D': 500,
8+
'M': 1000,
9+
# specify more numerals if you wish
10+
}
11+
12+
def RomanNumeralToDecimal(romanNumeral):
13+
sum = 0
14+
for i in range(len(romanNumeral) - 1):
15+
left = romanNumeral[i]
16+
right = romanNumeral[i + 1]
17+
if tallies[left] < tallies[right]:
18+
sum -= tallies[left]
19+
else:
20+
sum += tallies[left]
21+
sum += tallies[romanNumeral[-1]]
22+
return sum

0 commit comments

Comments
 (0)