Python Forum
Converting number to roman numerals
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting number to roman numerals
#3
Your Roman Numerals are in the wrong order. You provided a great example for how to solve the problem, then you wrote code that completely ignores the example.

In your example the first thing you did was find out how many 1000's there are in the number. For each 1000 you subtracted 1000 from the number and added 'M' to the Roman Numeral. Then you repeated this for the next lower value numeral (900 and CM). Repeat for every roman numeral in order of decreasing value until the remaining number is zero.

In your program you do the opposite. You start with the lowest value numeral work your way up. You know how to do this, don't freeze up just because you are now writing the algorithm in Python.

Maybe that is what you need, a written algorithm that can direct your coding.
def roman_numeral(number):
    # Map of roman numerals and values ordered in decreasing value.
    NUMERALS = {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'}
    # Start with an empty string
    retval = ''

    # Starting with the highest value, loop through all numerals
    ...:
        # While number is >= value, subtract value from number and add numeral to retval
        ...
    return retval
Next time write the comments yourself. If you have a hard time writing the comments it means you don't understand the problem. If you cannot write a good description of the algorithm you cannot write a program.
Reply


Messages In This Thread
Converting number to roman numerals - by jagasrik - Aug-08-2020, 08:54 PM
RE: Converting number to roman numerals - by deanhystad - Aug-09-2020, 04:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with implementation of the logic to convert roman numeral to integer jagasrik 2 2,371 Aug-14-2020, 07:31 PM
Last Post: deanhystad
  Roman Numeral Ordered List? (first post) CopperyMarrow15 1 1,792 Nov-06-2019, 11:06 PM
Last Post: Larz60+
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 9,360 Aug-06-2018, 05:19 PM
Last Post: vanicci

Forum Jump:

User Panel Messages

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