Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created January 9, 2018 14:26
Show Gist options
  • Save jrjames83/a382bfea37fd7aadac2a4f6245e7441b to your computer and use it in GitHub Desktop.
Save jrjames83/a382bfea37fd7aadac2a4f6245e7441b 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