Converts a string of roman numerals to an integer. e.g. >>> parse_roman_numerals("LXXXIX") 89 >>> parse_roman_numerals("IIII") 4
(numerals: str)
| 34 | |
| 35 | |
| 36 | def parse_roman_numerals(numerals: str) -> int: |
| 37 | """ |
| 38 | Converts a string of roman numerals to an integer. |
| 39 | e.g. |
| 40 | >>> parse_roman_numerals("LXXXIX") |
| 41 | 89 |
| 42 | >>> parse_roman_numerals("IIII") |
| 43 | 4 |
| 44 | """ |
| 45 | |
| 46 | total_value = 0 |
| 47 | |
| 48 | index = 0 |
| 49 | while index < len(numerals) - 1: |
| 50 | current_value = SYMBOLS[numerals[index]] |
| 51 | next_value = SYMBOLS[numerals[index + 1]] |
| 52 | if current_value < next_value: |
| 53 | total_value -= current_value |
| 54 | else: |
| 55 | total_value += current_value |
| 56 | index += 1 |
| 57 | total_value += SYMBOLS[numerals[index]] |
| 58 | |
| 59 | return total_value |
| 60 | |
| 61 | |
| 62 | def generate_roman_numerals(num: int) -> str: |