Basic Python Solution(easy to understand)


#1

def get_val®:
if(r==“I”):
return 1
elif(r==“V”):
return 5
elif(r==“X”):
return 10
elif(r==“L”):
return 50
elif(r==“C”):
return 100
elif(r==“D”):
return 500
elif(r==“M”):
return 1000

class Solution:
# @param A : string
# @return an integer
def romanToInt(self, A):

    fi=0
    n=len(A)
    l=[0]*n
    for i in range(n): 
        l[i]=A[i]
    
    n2=len(l)-1  
    if(n2==0):
        return get_val(l[0])
    for i in range(n2-1):
        if(get_val(l[i]) >= get_val(l[i+1])):
            fi+=get_val(l[i])
        else:
             fi-=get_val(l[i])
    if(get_val(l[n2-1]) >= get_val(l[n2])):
        fi+=get_val(l[n2-1])
        fi+=get_val(l[n2])
    else:
        fi-=get_val(l[n2-1])
        fi+=get_val(l[n2])
    return fi