python solution


  • 0
    G

    '''
    class main:

    '''takes a roman numeric string, replaces them with their corresponding
       numeric value and returns in the form of a list  '''
    def replace(self,roman):
        roman_list = list(roman)
        for i in range(0,len(roman_list)):
            if roman_list[i] == 'M':
                roman_list[i] = 1000
            elif roman_list[i] == 'D':
                roman_list[i] = 500
            elif roman_list[i] == 'C':
                roman_list[i] = 100            
            elif roman_list[i] == 'L':
                roman_list[i] = 50
            elif roman_list[i] == 'X':
                roman_list[i] = 10
            elif roman_list[i] == 'V':
                roman_list[i] = 5
            elif roman_list[i] == 'I':
                roman_list[i] = 1
        return roman_list
    
    '''Takes a list of numbers and calculates the value in the way people
    calculate roman numerals'''
    def calculate(self,roman_num):
        sumation = 0
        
        for j in range(0,len(roman_num),2):
            if j < len(roman_num)-1:
                if roman_num[j]<roman_num[j+1]:
                    sumation += (roman_num[j+1]-roman_num[j])
                elif roman_num[j]>=roman_num[j+1]:
                    sumation+=roman_num[j+1]+roman_num[j]
            elif len(roman_num)%2 != 0 and j == len(roman_num)-1:
                sumation += roman_num[j]
        return sumation
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.