Last active
January 17, 2017 16:35
-
-
Save sayuriTakeda/d675753680a72d6cad12cf007339e128 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
# coding=utf-8 | |
extense = [{ '0': ' ', '1': 'cento', '2': 'duzentos', '3': 'trezentos', | |
'4': 'quatrocentos', '5': 'quinhentos ', '6': 'seiscentos', '7': 'setecentos', | |
'8': 'oitocentos', '9': 'novecentos', '': ' ', ' ': ' ' | |
}, { | |
'0': ' ', '2': 'vinte', '3': 'trinta', '4': 'quarenta', '5': 'cinquenta', | |
'6': 'sessenta', '7': 'setenta', '8': 'oitenta', '9': 'noventa', | |
'': ' ', ' ': ' ' | |
}, { | |
'0': '', '1': 'um ', '2': 'dois ', '3': 'três ', '4': 'quatro', | |
'5': 'cinco', '6': 'seis', '7': 'sete', '8': 'oito', | |
'9': 'nove', '': ' ', ' ': ' ' | |
}] | |
single_milhars = [ '', 'mil', 'milhão', 'bilhão', 'trilhão', 'quadrilhão' ] | |
milhars = [ '', 'mil', 'milhões', 'bilhões', 'trilhões', 'quadrilhões' ] | |
tens_twenty_lower = { | |
'10': 'dez', '11': 'onze', '12': 'doze', '13': 'treze', '14': 'quatorze', '15': 'quinze', | |
'16': 'dezeseis', '17': 'dezessete', '18': 'dezoito', '19': 'dezenove' | |
} | |
def split_in_houses(int_num): | |
str_num = str(int_num) | |
return list(str_num) | |
def complete_with_zeros(num_list): | |
def mod_of_three(input_list): | |
return len(split_in_houses(input_list)) % 3 is 0 | |
if not mod_of_three(num_list): | |
not_complete = True | |
while(not_complete): | |
num_list.insert(0, '0') | |
if mod_of_three(num_list): | |
not_complete = False | |
return num_list | |
def split_in_hundreds(num_list): | |
digits = 3 | |
num_list = complete_with_zeros(num_list) | |
new_list = [num_list[n:n+digits] for n in range(0, len(num_list), digits)] | |
return new_list | |
def convert_in_tens(num_list): | |
if num_list[1] is '0': | |
return extense[2][num_list[2]] | |
if num_list[1] is '1': | |
tens = ''.join(num_list[1:]) | |
return tens_twenty_lower[tens] | |
if int(num_list[1]) > 1: | |
return "{} e {}".format(extense[1][num_list[1]], extense[2][num_list[2]]) | |
def convert_hundred(hundred_num_list): | |
full_hundred = hundred_num_list[0] | |
if full_hundred is '0': | |
return '' + convert_in_tens(hundred_num_list) | |
elif ''.join(hundred_num_list) == '100': | |
return 'cem' | |
else: | |
return "{} e {}".format(extense[0][full_hundred], convert_in_tens(hundred_num_list)) | |
def convert_into_thousands(number_matrix): | |
full_number = '' | |
milhar_houses = len(number_matrix) - 1 | |
for index, hundred_num in enumerate(number_matrix): | |
full_hundred = convert_hundred(hundred_num) | |
if 'um' in full_hundred: | |
full_number += "{} {} ".format( | |
full_hundred, | |
single_milhars[milhar_houses - index] | |
) | |
continue | |
if full_hundred is not '': | |
full_number += "{} {} ".format( | |
full_hundred, | |
milhars[milhar_houses - index] | |
) | |
return full_number | |
def convert(num): | |
return convert_into_thousands( | |
split_in_hundreds( | |
split_in_houses(num) | |
) | |
) | |
if __name__ == '__main__': | |
print('Digite um número') | |
converted = False | |
while not converted: | |
try: | |
print( | |
convert(input()) | |
) | |
converted = True | |
except (SyntaxError, NameError): | |
print('Digite somente números!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment