Python Forum
[Help] Convert integer to Roman numerals? {Screenshot attached}
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Help] Convert integer to Roman numerals? {Screenshot attached}
#11
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]
Blockchain Visionary & Aspiring Encipher/Software Developer
me = {'Python Learner' : 'Beginner\'s Level'}
http://bit.ly/JoinMeOnYouTube
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert 4 bytes to an integer ? GiggsB 11 6,603 Jan-20-2022, 03:37 AM
Last Post: GiggsB
  Help with implementation of the logic to convert roman numeral to integer jagasrik 2 2,253 Aug-14-2020, 07:31 PM
Last Post: deanhystad
  Converting number to roman numerals jagasrik 6 3,514 Aug-11-2020, 03:47 AM
Last Post: voidptr
  IEDriverServer screenshot ABVSVL 0 1,695 Jul-12-2020, 09:31 AM
Last Post: ABVSVL
  pyautogui.screenshot region is not working alexlu 6 6,388 Jun-04-2020, 10:46 AM
Last Post: alexlu
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,531 May-15-2020, 12:37 PM
Last Post: scidam
  Spawning a new process that is not attached to python cman234 3 1,843 Apr-25-2020, 05:24 PM
Last Post: cman234
  Roman Numeral Ordered List? (first post) CopperyMarrow15 1 1,687 Nov-06-2019, 11:06 PM
Last Post: Larz60+
  My objective is to get the shape parameters of the particles in attached image chad221 0 1,756 Oct-26-2019, 10:27 AM
Last Post: chad221
  Want to take a Screenshot from a File in Linux dhiliptcs 2 2,518 Oct-21-2019, 01:22 PM
Last Post: dhiliptcs

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020