How to convert integers to numerals

Jake

The following code only converts the integers to numerals up to 1010. At that point, an error starts to occur.

class Solution:
    def intToRoman(self, num: int) -> str:
        numerals = {
            1: "I", 
            4: "IV", 
            5: "V", 
            10: "X",
            40: "XL",
            50: "L", 
            90: "XC",
            100: "C", 
            400: "CD",
            500: "D", 
            900: "CM",
            1000: "M"
        }
        
        output = ""

        for k,v in sorted(numerals.items(), reverse=True):
            while num >= k:
                output += v
                num -= k
        
        return output
    

I am looping through the numerals dictionary from greatest to least number wise and getting the ints (v) and the numerals (k)

Then I am using a while loop to see if the number is >= value (v)

After that output will be added to the numerals (k), and number (num) will be subtracted from the integer (v).

Then I return value.

I was thinking of doing something like an if statement maybe like:

if num < v:
   # code

Whenever I try adding that, I get a "Time Limit Exceeded"

Tim Roberts

I'm not sure what you're seeing. When I fix your typos, this code produces good results.

However, in the interest of micro-optimization, I've replaced your unnecessary dictionary with a tuple that is already in order:

class Solution:
    numerals = (
        ("M", 1000),
        ("CM", 900),
        ("D", 500), 
        ("CD", 400), 
        ("C", 100), 
        ("XC", 90),
        ("L", 50), 
        ("XL", 40), 
        ("X", 10), 
        ("IX", 9),
        ("V", 5), 
        ("IV", 4), 
        ("I", 1), 
    )
        
    def intToRoman(self, num: int) -> str:
        output = ""

        for k,v in self.numerals:
            while num >= v:
                output += k 
                num -= v
        
        return output

s = Solution()
print( s.intToRoman( 10 ) )
print( s.intToRoman( 19 ) )
print( s.intToRoman( 4345 ) )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert Roman numerals into decimal numbers?

How can I convert Persian numerals in UTF-8 to European numerals in ASCII?

Converting Roman Numerals to Integers in Java

convert number into roman numerals

Convert number to Roman numerals

Convert arabic numerals in roman numerals in pure CSS

How to convert an array of integers to a tree?

How to convert French words to integers?

How to convert strings into integers in Python?

How to convert an array of integers to an integer

How to convert list of integers to image?

How to convert integers to characters in C?

Roman Numerals to Integers Converter with Python using a dictionary

Converting roman numerals to integers in a list of lists

Defining a function to represent integers in Church numerals (DrRacket)

Convert roman numerals to numbers in R

C# function to convert Numerals

How to convert a list of integers into a matrix of these integers in F#

How do I convert a matrix of integers to a matrix of lists of integers in numpy?

How to convert an array of string integers to an array of integers? Ruby

How to convert Roman numerals to int while rejecting invalid numbers using standard C?

How to convert a string representation to an array integers?

How to convert numbers and letters a string in a list into integers?

How to convert the values of CheckBoxes into a sum of integers?

How to convert one-hot encodings into integers?

How to convert vector of integers to and from bytes?

How to convert integers to pointers and vice versa

How to convert a list of multiple integers into a single integer?

How do I convert a list of integers into bytes?