Python Forum

Full Version: [Help] Convert integer to Roman numerals? {Screenshot attached}
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Thank you @Windspar & @ichabod801 for your amazing feedback! :)

While waiting for your responses I managed to figure things out and finally make it work. I had a pair programming session with someone who has a basic understanding of python and we both learned new things!

Will show you my code for both Old-school Roman numerals and Modern Roman numerals exercises [below].

===================================================================
# Old-school Roman numerals. In the early days of Roman numerals, the Romans didn’t bother with any of this new-fangled subtraction “IX” nonsense. No Mylady, it was straight addition, biggest to littlest—so 9 was written “VIIII,” and so on. Write a method that when passed an integer between 1 and 3000 (or so) returns a string containing the proper old-school Roman numeral. In other words, old_roman_numeral 4 should return 'IIII'. Make sure to test your method on a bunch of different numbers.

Hint: Use the integer division and modulus methods.

For reference, these are the values of the letters used: I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000

print("[System] Hi! Enter any number between 1 and 3000 so the program will show you the Old-school Roman numeral. In other words, 4 should return 'IIII'. Ready?!")

num_map = [(1000, 'M'), (500, 'D'), (100, 'C'), (50, 'L'), (10, 'X'), (5, 'V'), (1, 'I')]

num = int(input("Enter any number between 1 and 3000: "))

def int_to_roman(num):
    result = []
    for (arabic, roman) in num_map:
        (factor, num) = divmod(num, arabic)
        result.append(roman * factor)
        if num == 0:
            break
    return "".join(result)

print(int_to_roman(num))
===================================================================
# “Modern” Roman numerals. Eventually, someone thought it would be terribly clever if putting a smaller number before a larger one meant you had to subtract the smaller one. As a result of this development, you must now suffer. Rewrite your previous method to return the new-style Roman numerals so when someone calls roman_numeral 4, it should return 'IV', 90 should be 'XC' etc.

print("[System] Hi! Enter any number between 1 and 3000 so the program will show you the Modern Roman numeral. In other words, 4 should return 'IV'. Ready?!")

num_map = [(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')]

num = int(input("Enter any number between 1 and 3000: "))

def int_to_roman(num):
    result = []
    for (arabic, roman) in num_map:
        (factor, num) = divmod(num, arabic)
        result.append(roman * factor)
        if num == 0:
            break
    return "".join(result)

print(int_to_roman(num))
===================================================================
My terminal's screenshot

[Image: 2_zpspcs5errv.png]
Pages: 1 2