Created
January 18, 2017 16:25
-
-
Save MinimumViablePerson/972a607e8c5d1f94e73f260a9fcf2ccd to your computer and use it in GitHub Desktop.
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
def number_to_roman(number): | |
"""Convert a number between 1 and 3,999,999 to roman numerals.""" | |
ROMAN_ONES = ['I', 'X', 'C', 'M', 'X̅', 'C̅', 'M̅'] | |
ROMAN_FIVES = ['V', 'L', 'D', 'V̅', 'L̅', 'D̅'] | |
conversion = "" | |
if not str(number).isnumeric(): | |
return "Only numbers are allowed." | |
if int(number) > 3999999: | |
return "Please enter a number lower than 3,999,999." | |
for index, number in enumerate(str(number)[::-1]): | |
number = int(number) | |
if number in range(1, 4): | |
conversion += ROMAN_ONES[index] * number | |
elif number == 4: | |
conversion += ROMAN_FIVES[index] + ROMAN_ONES[index] | |
elif number == 5: | |
conversion += ROMAN_FIVES[index] | |
elif number in range(6, 9): | |
conversion += ROMAN_ONES[index] * (number % 5) + ROMAN_FIVES[index] | |
elif number == 9: | |
conversion += ROMAN_ONES[index + 1] + ROMAN_ONES[index] | |
return conversion[::-1] | |
if __name__ == '__main__': | |
number = input('Enter a number: ') | |
print(number_to_roman(number)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment