[leetcode simple] Roman numeral to integer

Keywords: Python

Roman numerals contain the following seven characters: I, V, X, L, C, D, and M.

Character value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, the Roman numeral 2 is written as II, that is, two parallel ones. 12 written as XII, that is, X + II. 27 written as XXVII, i.e. XX + V + II.

Usually, the small numbers in roman numbers are to the right of the big ones. But there are also special cases. For example, 4 does not write IIII, but IV. The number 1 is to the left of the number 5. The number represented is equal to the number 4 obtained by subtracting the decimal 1 from the large number 5. Similarly, the number 9 is expressed as IX. This special rule only applies to the following six cases:

  • I can be placed to the left of V (5) and X (10) to represent 4 and 9.
  • X can be placed to the left of L (50) and C (100) to represent 40 and 90.  
  • C can be placed to the left of D (500) and M (1000) to represent 400 and 900.

Given a Roman number, convert it to an integer. Enter to make sure it is in the range 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
 Explanation: C = 100, L = 50, XXX = 30, III = 3

Example 5:

Input: "MCMXCIV"
Output: 1994
 Explanation: M = 1000, CM = 900, XC = 90, IV = 4

def result():
    total=0
    src_dict={'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900,'I':1,'V':5,'X':10, 'L':50,'C':100,'D':500,'M':1000}
    while True:
        next = yield
        if next is None:
            break
        total+=src_dict[next]
    return total

def gather(gather_list):
    while True:
        gather_list['sum'] = yield from result()

class Solution:
    def romanToInt(self,s):
        gather_list = {'sum':0}
        values=gather(gather_list)
        next(values)
        new_s=[]
        len_s=len(s)
        start = 0
        for i in range(len_s):
            if start < len_s-1:
                if s[start] == 'I' and s[start + 1] == 'V':
                    new_s.append('IV')
                    start+=2
                elif s[start] == 'I' and s[start + 1] == 'X':
                    new_s.append('IX')
                    start += 2
                elif s[start] == 'X' and s[start + 1] == 'L':
                    new_s.append('XL')
                    start += 2
                elif s[start] == 'X' and s[start + 1] == 'C':
                    new_s.append('XC')
                    start += 2
                elif s[i] == 'C' and s[i + 1] == 'D':
                    new_s.append('CD')
                    start += 2
                elif s[i] == 'C' and s[i + 1] == 'M':
                    new_s.append('CM')
                    start += 2
                else:
                    new_s.append(s[start])
                    start+=1
            if start == len_s - 1:
                new_s.append(s[start])
                start += 1
        for value in new_s:
            values.send(value)
        values.send(None)
        return gather_list['sum'] if 1 <= gather_list['sum'] <=3999 else False

 




Posted by dhie on Tue, 07 Jan 2020 08:01:18 -0800