-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRomanToInteger.py
More file actions
executable file
·34 lines (27 loc) · 952 Bytes
/
Copy pathRomanToInteger.py
File metadata and controls
executable file
·34 lines (27 loc) · 952 Bytes
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
# -*- coding: UTF-8 -*-
# Given a roman numeral, convert it to an integer.
#
# Input is guaranteed to be within the range from 1 to 3999.
#
# Python, Python 3 all accepted.
class RomanToInteger(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
if s is None:
return 0
dictionary = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
char_list = list(s)
result = 0
i = 0
while i < len(char_list):
if i + 1 < len(char_list) and dictionary[char_list[i + 1]] > dictionary[char_list[i]] and (
char_list[i] == 'I' or char_list[i] == 'X' or char_list[i] == 'C'):
result += dictionary[char_list[i + 1]] - dictionary[char_list[i]]
i += 1
else:
result += dictionary.get(char_list[i])
i += 1
return result