Created
November 4, 2020 06:36
-
-
Save baileywickham/383fae48f1a808d2e143aa225a45496e to your computer and use it in GitHub Desktop.
Roman numeral converter
This file contains hidden or 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
rmn = {1000 : 'M', | |
900 : 'CM', | |
500 : 'D', | |
400 : 'CD', | |
100 : 'C', | |
90 : 'XC', | |
50 : 'L', | |
40 : 'XL', | |
10 : 'X', | |
9 : 'IX', | |
5 : 'V', | |
4 : 'IV', | |
1 : 'I'} | |
def roman(n): | |
s = '' | |
# This relies on behavior undefined before python 3.9. | |
for k in rmn.keys(): | |
while n - k >= 0: | |
s = s + rmn[k] | |
n = n - k | |
return s | |
assert roman(5) == 'V' | |
assert roman(3549) == "MMMDXLIX" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment