Skip to content

Instantly share code, notes, and snippets.

@hoogenm
Created July 9, 2017 15:30
Show Gist options
  • Save hoogenm/f1de56a194c92c9f6989188c13475f92 to your computer and use it in GitHub Desktop.
Save hoogenm/f1de56a194c92c9f6989188c13475f92 to your computer and use it in GitHub Desktop.
Roman numerals (Roman numbers) to integer (concisely in Python)
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