Created
September 15, 2017 23:35
-
-
Save oliveiraev/ca0cfd5f8f597ffd1db1d41b3864958a to your computer and use it in GitHub Desktop.
Imprimidor de numeral ordinal.
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
# -*- coding: utf-8 -*- | |
u"""Imprimidor de numeral ordinal.""" | |
import sys | |
UNITS = [ | |
'', | |
'primeiro', | |
'segundo', | |
'terceiro', | |
'quarto', | |
'quinto', | |
'sexto', | |
'sétimo', | |
'oitavo', | |
'nono', | |
] | |
TENTHS = [ | |
'', | |
'décimo', | |
'vigésimo', | |
'trigésimo', | |
'quadragésimo', | |
'quiquagésimo', | |
'sexagésimo', | |
'septagésimo', | |
'octogésimo', | |
'nonagésimo' | |
] | |
CENTS = [ | |
'', | |
'centésimo', | |
'ducentésimo', | |
'tricentésimo', | |
'quadrigentésimo', | |
'quingentésimo', | |
'seiscentésimo', | |
'septingentésimo', | |
'octingentésimo', | |
'nongentésimo', | |
] | |
def ordinal(number): | |
u""" | |
Converte numeral em ordinal. | |
>>> ordinal(1) | |
'primeiro' | |
>>> ordinal(18) | |
'décimo oitavo' | |
""" | |
try: | |
number = int(number) | |
except (ValueError, TypeError): | |
raise TypeError("Numeral inválido: {}".format(number)) | |
if number > 999: | |
raise ValueError("Só sei ordenar até 999!!") | |
names = [CENTS, TENTHS, UNITS] | |
# garantindo que 'number' tenha pelo menos 3 items | |
number = [0, 0] + [int(part) for part in list(str(number))] | |
parts = [names[part][value] for part, value in enumerate(number[-3:])] | |
return " ".join([part for part in parts if part]) | |
if __name__ == "__main__": | |
for number in sys.argv[1:]: | |
print(ordinal(number)) | |
print("Hoje é o {} dia do ano. Parabéns programador!".format(ordinal(256))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment