-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
57 lines (44 loc) · 1.19 KB
/
helpers.py
File metadata and controls
57 lines (44 loc) · 1.19 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Python program to convert Roman Numerals
# to Numbers
def roman_to_decimal(roman):
def value(r):
# This function returns value of each Roman symbol
if r == 'I':
return 1
if r == 'V':
return 5
if r == 'X':
return 10
if r == 'L':
return 50
if r == 'C':
return 100
if r == 'D':
return 500
if r == 'M':
return 1000
return -1
roman = str(roman)
res = 0
i = 0
while i < len(roman):
# Getting value of symbol s[i]
s1 = value(roman[i])
if i + 1 < len(roman):
# Getting value of symbol s[i+1]
s2 = value(roman[i + 1])
# Comparing both values
if s1 >= s2:
# Value of current symbol is greater
# or equal to the next symbol
res = res + s1
i = i + 1
else:
# Value of current symbol is greater
# or equal to the next symbol
res = res + s2 - s1
i = i + 2
else:
res = res + s1
i = i + 1
return res