Skip to content

Instantly share code, notes, and snippets.

@pepasflo
Created August 2, 2018 21:52
Show Gist options
  • Save pepasflo/5acb3bebe3c4852eea0177b61cc348c5 to your computer and use it in GitHub Desktop.
Save pepasflo/5acb3bebe3c4852eea0177b61cc348c5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
table = (
("M", 1000),
("D", 500),
("CD", 400),
("C", 100),
("L", 50),
("XL", 40),
("X", 10),
("IX", 9),
("V", 5),
("IV", 4),
("I", 1),
)
def roman(i, table=table):
if i == 0: return ""
if table == (): return ""
(symbol, value) = table[0]
quotient = i / value
if quotient > 0:
result = quotient * symbol
else:
result = ""
return result + roman(i - (quotient * value), table[1:])
for i in [1,2,3,4,5,6,7,8,9,10,11,14,15,16,49,50,51]:
print "roman(%s): %s" % (i, roman(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment