def roman_to_int(expr: str):
    """ Convert a Roman numeral to an integer.
    Adopted from https://www.oreilly.com/library/view/python-cookbook/0596001673/ch03s24.html
    """
    expr = expr.upper()
    nums = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
    sum = 0
    for i in range(len(expr)):
        try:
            value = nums[expr[i]]
            # If the next place holds a larger number, this value is negative
            if i+1 < len(expr) and nums[expr[i+1]] > value:
                sum -= value
            else: sum += value
        except KeyError:
            raise ValueError(f'expr is not a valid Roman numeral:{expr}')
    return sum