Skip to content

Instantly share code, notes, and snippets.

@fabiom
Last active June 13, 2020 21:06
Show Gist options
  • Save fabiom/692a3a03162a1cdf05b25ca0dbbf6b57 to your computer and use it in GitHub Desktop.
Save fabiom/692a3a03162a1cdf05b25ca0dbbf6b57 to your computer and use it in GitHub Desktop.
Display integers using roman numerals.
#!/usr/bin/env python3
# Run `import roman` then you can use the function `roman.rnumeral()`.
def rnumeral(n):
if n >= 4000:
print('The largest number that can be represented in roman numerals is 3999.')
return
if n <= 0:
print('Choose a positive number.')
return
num = str(n)
Rom = ('I', 'V', 'X', 'L', 'C', 'D', 'M')
res = ''
count = (len(num)-1)*2
for c in num:
M = int(c)
if (M > 0 and M < 4):
res = res + M*Rom[count]
if (M >= 4 and M <= 5):
res = res + (5 % M)*Rom[count] + Rom[count+1]
if (M > 5 and M < 9):
res = res + Rom[count+1] + (M % 5)*Rom[count]
if (M >= 9 and M <= 10):
res = res + (M % 2)*Rom[count] + Rom[count+2]
count = count - 2
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment