Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic program to convert integer to Roman numerals?

Tags:

python

I'm trying to write a code that converts a user-inputted integer into its Roman numeral equivalent. What I have so far is:

screenshot of my code

The point of the generate_all_of_numeral function is so that it creates a string for each specific numeral. For example, generate_all_of_numeral(2400, 'M', 2000) would return the string 'MM'.

I'm struggling with the main program. I start off finding the Roman numeral count for M and saving that into the variable M. Then I subtract by the number of M's times the symbol value to give me the next value to work with for the next largest numeral.

Any nod to the right direction? Right now my code doesn't even print anything.

like image 867
skybreaker Avatar asked Feb 28 '15 01:02

skybreaker


People also ask

How do you convert int to Roman numerals in Python?

Create a lookup list containing tuples in the form of (roman value, integer). Use a for loop to iterate over the values in lookup. Use divmod() to update num with the remainder, adding the roman numeral representation to the result.

How do you convert int to Roman numerals in C++?

We take the corresponding roman value from the map and append it to our answer. At first, we process 6 thus ans = VI and our integer becomes 240. Second, we process 40 thus ans = XLVI and the integer become 200. Third, we process 200 and ans = CCXLVI and our loop terminates.

How do I convert numbers to Roman numerals in Excel?

What is an example of how to use ROMAN in Excel? To input the Roman numeral "IV" into Excel, type "=ROMAN(4)" into a cell. Excel will return the Roman numeral "IV" as the result.


2 Answers

A KISS version of Manhattan's algorithm, without any "advanced" notion such as OrderedDict, recursion, generators, inner function and break:

ROMAN = [
    (1000, "M"),
    ( 900, "CM"),
    ( 500, "D"),
    ( 400, "CD"),
    ( 100, "C"),
    (  90, "XC"),
    (  50, "L"),
    (  40, "XL"),
    (  10, "X"),
    (   9, "IX"),
    (   5, "V"),
    (   4, "IV"),
    (   1, "I"),
]

def int_to_roman(number):
    result = ""
    for (arabic, roman) in ROMAN:
        (factor, number) = divmod(number, arabic)
        result += roman * factor
    return result

An early exit could be added as soon as number reaches zero, and the string accumulation could be made more pythonic, but my goal here was to produce the requested basic program.

Tested on all integers from 1 to 100000, which ought to be enough for anybody.

EDIT: the slightly more pythonic and faster version I alluded to:

def int_to_roman(number):
    result = []
    for (arabic, roman) in ROMAN:
        (factor, number) = divmod(number, arabic)
        result.append(roman * factor)
        if number == 0:
            break
    return "".join(result)
like image 141
Aristide Avatar answered Nov 01 '22 23:11

Aristide


Here's what I tried:

def int_to_roman(num):
    dict_of_known_val = {1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C',
                         90: 'XC', 50: 'L', 40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'}
    result = ''
    for key, value in dict_of_known_val.items():
        x, y = divmod(num, key)
        result += value * x
        num = y
    return result

This code works for me.

like image 36
vivek kumar Avatar answered Nov 02 '22 00:11

vivek kumar