Skip to content

Instantly share code, notes, and snippets.

@kkoci
Forked from jrjames83/roman_numerals.py
Created January 6, 2019 22:24
Show Gist options
  • Save kkoci/a0b73d4ee01b5dd65ea190377602fa2f to your computer and use it in GitHub Desktop.
Save kkoci/a0b73d4ee01b5dd65ea190377602fa2f to your computer and use it in GitHub Desktop.
Roman Numerals Kata - Python 3.6 Dict keys retain input ordering
numerals = {"I":1, "V":5, "X":10, "L": 50, "C": 100, "D": 500, "M": 1000 }
def convert_roman(input_number):
range_flag = None
for symbol, integer in numerals.items():
if integer == input_number: return symbol
if input_number > integer:
range_flag = symbol
remaining = input_number - numerals[range_flag]
return range_flag + convert_roman(remaining)
print(convert_roman(1)) # MCXI
print(convert_roman(17)) # XVII
print(convert_roman(1111)) # MCXI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment