Created
February 20, 2013 00:29
-
-
Save luw2007/4991617 to your computer and use it in GitHub Desktop.
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
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