Skip to content

Commit b60474d

Browse files
committed
script to convert roman numerals into ints
1 parent 64eb00b commit b60474d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

scripts/roman.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env python
2+
##
3+
# Convert roman numerals into ints
4+
# Created by Joseph Walton-Rivers
5+
##
6+
##
7+
# Useage: ./roman.py and follow the prompt
8+
##
9+
10+
roman = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
11+
roman_input = raw_input("Enter Roman Numeral: ")
12+
13+
def main(roman_input):
14+
lowest = 1000
15+
16+
total = 0
17+
skip = []
18+
for x in range(0, len(roman_input)):
19+
roman_value = roman_input[x]
20+
int_value = roman[roman_value]
21+
22+
# Subtraction rule
23+
next_roman_value = roman_input[x+1] if x+1 < len(roman_input) else ''
24+
next_int_value = roman[next_roman_value] if next_roman_value != '' else 0
25+
if int_value < next_int_value:
26+
int_value = (next_int_value - int_value)
27+
skip.append(x+1)
28+
29+
# Skip the value if it's part of a subsitution rule
30+
if x not in skip:
31+
total = total + int_value
32+
33+
# ERROR CHECK: the number should always decrease or be equal to the last
34+
if lowest < int_value:
35+
print ("ERROR - value goes up!")
36+
return 0
37+
lowest = int_value
38+
return total
39+
40+
print ("the total is "+str(main(roman_input)))

0 commit comments

Comments
 (0)