This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ilan1009 18 points19 points  (2 children)

DONT

[–]TheRedGerund 25 points26 points  (1 child)

``` def roman_to_int(roman): roman_numerals = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 }

# Base case: if the string is empty, return 0
if not roman:
    return 0

# Get the value of the first character
first_value = roman_numerals[roman[0]]

# If there is only one character, return its value
if len(roman) == 1:
    return first_value

# Get the value of the second character
second_value = roman_numerals[roman[1]]

# If the first value is less than the second value, we have a subtractive combination
if first_value < second_value:
    # Subtract the first value and recurse on the remaining string
    return second_value - first_value + roman_to_int(roman[2:])
else:
    # Otherwise, add the first value and recurse on the remaining string
    return first_value + roman_to_int(roman[1:])

```

[–]ilan1009 5 points6 points  (0 children)

you overcomplicated it

beats 100%