123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import re
- import pytest
- def romanToInt(s:str):
- special = {'IV': 4,
- 'IX': 9,
- 'XL': 40,
- 'XC': 90,
- 'CD': 400,
- 'CM': 900}
- single = {'I': 1,
- 'V': 5,
- 'X': 10,
- 'L': 50,
- 'C': 100,
- 'D': 500,
- 'M': 1000}
- r = 0
- for k, v in special.items():
- if k in s:
- r += v
- s = re.sub(k, '', s)
- print(s)
- for i in s:
- r += single[i]
- return r
- @pytest.mark.parametrize(
- "s, expect",
- [
- ('IV', 4),
- ('III', 3),
- ('LVIII', 58),
- ('MCMXCIV', 1994)
- ]
- )
- def test_cases(s, expect):
- assert romanToInt(s) == expect
|