Created
July 9, 2017 15:30
-
-
Save hoogenm/f1de56a194c92c9f6989188c13475f92 to your computer and use it in GitHub Desktop.
Roman numerals (Roman numbers) to integer (concisely in Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def roman_to_int(roman_string): | |
symbol_groups = [{'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900}, | |
{'M': 1000,'D': 500 ,'C': 100,'L': 50,'X': 10,'V': 5,'I': 1}] | |
result = 0 # initially | |
for symbol_group in symbol_groups: | |
for symbol, value in symbol_group.items(): | |
result += roman_string.count(symbol) * value | |
roman_string = roman_string.replace(symbol, '') | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment