Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python Roman to Integer

class Solution:
    def romanToInt(self, s):
 
        values = {'I': 1, 'V': 5, 'X': 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}   
        result = 0
        for i in range(len(s)):
            if i + 1 < len(s) and values[s[i]] < values[s[i + 1]] : # if current item is not the last item on the string
                                                                    # and current item's value is smaller than next item's value 
                result = result - values[s[i]]                      # then subtract current item's value from result
            else:
                result = result + values[s[i]]                      # otherwise add current item's value to result
        return result

Task = Solution()
print(Task.romanToInt("III"))
print(Task.romanToInt("LVIII"))
print(Task.romanToInt("MCMXCIV"))
Comment

python roman to integer

def roman_to_int(roman:str) -> int:
    romans = {
        "I": 1,
        "V": 5,
        "X": 10,
        "L": 50,
        "C": 100,
        "D": 500,
        "M": 1000
    }
    result = 0
    
    # Single digit number
    if len(roman) < 2:
        return romans[roman.upper()]
      
    # Iterate the string only once -> O(n)
    for i in range(len(roman)-1):
        current_num = romans[roman[i].upper()]
        next_num = romans[roman[i+1].upper()]
        if current_num < next_num:
            result -= current_num
        else:
            result += current_num
    else:  # Add the last number
        result += next_num

    return result
Comment

roman to integer python

def romanToInt(rm_letter):
    roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900}
    i = 0
    num = 0
    while i < len(rm_letter):
        if i+1<len(rm_letter) and rm_letter[i:i+2] in roman:
            num+=roman[rm_letter[i:i+2]]
            i+=2
        else:

            num+=roman[rm_letter[i]]
            i+=1
    return num

roman = input("Enter roman letter: ").upper()
roman=romanToInt(roman)
print(roman)
Comment

Roman to integer with python

def romanToInt(self, s: str) -> int:
    roman = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
    result = 0

    for i in range(len(s)):
        if i + 1 < len(s) and roman[s[i]] < roman[s[i+1]]:
            result -= roman[s[i]]
        else: 
            result += roman[s[i]]
    return result
Comment

Python Roman to Integer method 2

class Solution:
    def romanToInt(self, s):
 
        values = {'I': 1, 'V': 5, 'X': 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}   
        result = 0
        for i in range(len(s)):
            if i + 1 == len(s) or values[s[i]] >= values[s[i + 1]] : # if current item is not the last item on the string
                                                                    # or current item's value is greater than or equal to next item's value 
                result = result + values[s[i]]                      # then add current item's value from result
            else:
                result = result - values[s[i]]                      # otherwise subtract current item's value from result
        return result

Task = Solution()
print(Task.romanToInt("III"))
print(Task.romanToInt("LVIII"))
print(Task.romanToInt("MCMXCIV"))
Comment

PREVIOUS NEXT
Code Example
Python :: fasttext python 
Python :: finding path of a module in python 
Python :: custom jupyter notebook 
Python :: increase axis ticks pyplot 
Python :: numpy int64 to int 
Python :: position in array python 
Python :: run for loop inside pdb 
Python :: python sns lable axes 
Python :: group by in ruby mongoid 
Python :: difference between set and tuple in python 
Python :: tkinter window size 
Python :: how to use label encoding in python 
Python :: startapp django 
Python :: import get user model django 
Python :: how to reverse array in python 
Python :: how to add the sum of multiple columns into another column in a dataframe 
Python :: play video in colab 
Python :: python series 
Python :: run in another thread decorator 
Python :: python offline translate pypi 
Python :: What is role of ALLOWED_HOSTs in Django 
Python :: How to remove all characters after a specific character in python? 
Python :: code fibonacci python 
Python :: python f string 2 decimals 
Python :: not null constraint failed django 
Python :: how to make a checksum to a file python 
Python :: slicing in python listing 
Python :: cors flask 
Python :: how to make gtts text to speech converter 
Python :: delete from list python 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =