Created
January 9, 2018 14:26
-
-
Save jrjames83/a382bfea37fd7aadac2a4f6245e7441b to your computer and use it in GitHub Desktop.
Roman Numerals Kata - Python 3.6 Dict keys retain input ordering
This file contains 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
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