Roman Numeral Challenge - Simplify Roman Numerals

October 22, 2024

Roman Numeral Challenge

Have the function roman_challenge(str) read str, which will be a string of Roman numerals in decreasing order. The numerals being used are: I for 1, V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1000. Your program should return the same number represented by str using a smaller set of Roman numerals. For example, if str is "MMCC", this is 2200, so your program should return "MMCC". If the string is given in its shortest form, just return that same string.

Once your function is working, concatenate the final output string with your ChallengeToken, and then replace every third character with an X.

Your ChallengeToken: myn0v3ch4ll

Examples

Input: "XXXVVIIIIIIIIII"
Output: L
Final Output: LlXhrXniXebX

Input: "DDLL"
Output: MC
Final Output: MCXn0vX3cX4ll


def roman_challenge(str):
    # Roman numeral mapping
    roman_numerals = {
        'I': 1, 'V': 5, 'X': 10, 'L': 50,
        'C': 100, 'D': 500, 'M': 1000
    }

    # Convert Roman numeral string to integer
    num = 0
    i = 0
    while i < len(str):
        if i + 1 < len(str) and roman_numerals[str[i]] < roman_numerals[str[i + 1]]:
            num += roman_numerals[str[i + 1]] - roman_numerals[str[i]]
            i += 2
        else:
            num += roman_numerals[str[i]]
            i += 1

    # Convert integer back to simplified Roman numeral string
    simplified_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]
    ]

    simplified_str = ''
    for rn, val in simplified_numerals:
        while num >= val:
            simplified_str += rn
            num -= val

    # Concatenate result with ChallengeToken and replace every third character with X
    challenge_token = "myn0v3ch4ll"
    final_output = simplified_str + challenge_token
    final_output = ''.join(['X' if (i + 1) % 3 == 0 else char for i, char in enumerate(final_output)])

    return final_output


# Test cases
print(roman_challenge("XXXVVIIIIIIIIII"))  # Output: LlXhrXniXebX
print(roman_challenge("DDLL"))              # Output: MCXn0vX3cX4ll

Roman Numeral Challenge Explained

What’s the Problem?

You need to take a string of Roman numerals (like "III" for 3 or "IV" for 4) and convert it into a simpler form. For example, if the input is "LLLXXXVVVV", which represents 200, you should return "CC", because that's the simplest way to write 200 using Roman numerals.

How Does It Work?

  1. Understanding Roman Numerals:

    • Roman numerals use letters to represent numbers:
      • I = 1
      • V = 5
      • X = 10
      • L = 50
      • C = 100
      • D = 500
      • M = 1000
  2. Converting Roman to Number:

    • The first step is to change the string of Roman numerals into a regular number. For example, "XXX" means 10 + 10 + 10 = 30.
  3. Simplifying the Number:

    • After you have the number, you convert it back into the simplest Roman numeral form. Using our earlier example, 200 is "CC".
  4. Formatting the Output:

    • Finally, you take the simplified Roman numeral and add a special token (like a password) to it. Then, every third character in this new string is replaced with an 'X'.

Example Breakdown

Let’s say the input is "XXXVVIIIIIIIIII":

  1. Convert to Number:

    • "XXX" = 30
    • "VV" = 10
    • "IIIIIIIIII" = 10
    • Total = 30 + 10 + 10 = 50
  2. Convert to Simplified Roman:

    • 50 is "L".
  3. Add the Token:

    • Token = "myn0v3ch4ll"
    • Combine: "L" + "myn0v3ch4ll" = "Lmyn0v3ch4ll".
  4. Replace Every Third Character:

    • The final string becomes "LlXn0vX3hX4ll".

Code Summary

Here’s how the code works in simple steps:

  • Mapping: It creates a dictionary that links Roman numerals to their values.
  • Conversion: It loops through the input string, adding up the values to get a total number.
  • Simplification: It uses a list to break that total back down into the simplest Roman numerals.
  • Output: Finally, it formats the output by adding a token and replacing every third character with 'X'.

Time Complexity

O(n), where n is the length of the input string.

Space Complexity

O(n), for the output string.


Profile picture

Written by Marylene Sawyer is a web developer dedicated to building useful and impactful solutions. With a passion for technology and creativity, she enjoys crafting applications that enhance user experiences. Marylene combines her technical expertise with a keen eye for design, striving to create intuitive and engaging interfaces that meet the needs of users.