Skip to content

Instantly share code, notes, and snippets.

@luw2007
Created February 20, 2013 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luw2007/4991617 to your computer and use it in GitHub Desktop.
Save luw2007/4991617 to your computer and use it in GitHub Desktop.
def checkio(n):
'return roman numeral using the specified integer value from range 1...3999'
if 1 <= n <= 3999:
return ''.join(to_roman(n))
def to_roman(num):
roman = [["","I","II","III","IV","V","VI","VII","VIII","IX"],
["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"],
["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"],
["","M","MM","MMM"]]
i= 10000
out = []
for j in range(4):
n = (num%i) / (i/10)
i/=10
if roman[3-j][n]:
out.append(roman[3-j][n])
return out
if __name__ == '__main__':
assert checkio(6) == 'VI', 'First'
assert checkio(76) == 'LXXVI', 'Second'
assert checkio(499) == 'CDXCIX', 'Third'
assert checkio(3888) == 'MMMDCCCLXXXVIII', 'Fourth'
print 'All ok'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment