Created
January 17, 2012 00:41
-
-
Save yngwie74/1623830 to your computer and use it in GitHub Desktop.
Mi implementación de la kata "Roman Numerals" en una sola función.
This file contains 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
Implementación de la kata "Roman Numerals" (http://bit.ly/92cEtq). | |
Esta es una implementación "literal" de las reglas de la numeración romana. | |
Fue escrita con el propósito específico de *NO* usar un diccionario en la | |
conversión. | |
""" | |
_SIMBOLS = ('IVX', 'XLC', 'CDM', 'MMM') | |
def romanOf(number): | |
result = [] | |
for (one, five, ten) in _SIMBOLS: | |
number, digit = divmod(number, 10) | |
if digit <= 3: | |
numeral = one * digit | |
elif digit <= 5: | |
numeral = one * (5 - digit) + five | |
elif digit < 9: | |
numeral = five + one * (digit - 5) | |
else: | |
numeral = one + ten | |
result.append(numeral) | |
return ''.join(result[::-1]) | |
if __name__ == '__main__': | |
print romanOf(1999), '-> MCMXCIX' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment