torchcell.sequence.roman_to_int

roman_to_int(s: str) → int[source]

Converts a Roman numeral string to an integer.

This function interprets the given string s as a Roman numeral and returns its value as an integer. It handles the standard Roman numeral symbols (I, V, X, L, C, D, M) and uses the subtractive notation rule, where placing a smaller numeral to the left of a larger numeral represents subtraction (e.g., IV for 4).

Parameters:

s (str) – The Roman numeral string to convert, consisting of the characters I, V, X, L, C, D, M. It is assumed to be a valid Roman numeral.

Returns:

The integer value of the Roman numeral.

Return type:

int

Raises:

KeyError – If input string contains characters without valid Roman numeral.

Example

>>> roman_to_int("IV")
4
>>> roman_to_int("IX")
9
>>> roman_to_int("XIII")
13