Skip to content

Instantly share code, notes, and snippets.

@baileywickham
Created November 4, 2020 06:36
Show Gist options
  • Save baileywickham/383fae48f1a808d2e143aa225a45496e to your computer and use it in GitHub Desktop.
Save baileywickham/383fae48f1a808d2e143aa225a45496e to your computer and use it in GitHub Desktop.
Roman numeral converter
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