Function that returns uppercase consonants

Asked

Viewed 329 times

2

the function receives a sentence as input and returns the sentence with all its consonants in uppercase.

def teste(frase):
    i=0
    while i<len(frase):
        if frase[i]in 'bcdfghjklmnpqrstvxwyz':
            str.upper(frase[i])
        i=i+1
    return frase

I’m trying with this code but instead of returning only the uppercase consonants, it returns the normal sentence. Obs: needs to be using while

2 answers

3

An alternative may be the use of the method str.translate() which returns a copy of the string in which each character was mapped through the translation table. This translation table can be constructed with the method static str.maketrans() whose purpose is returns a translation table usable for str.translate(). The method static str.maketrans() can be invoked with one to three arguments but in this specific case what matters is the call with two arguments that must be strings of equal length where each character in the first argument will be mapped to the character in the same position in the second argument.

A consideration that must be made about this conversion is the special case of the letter C because it has a variation of C.

Exemlplo:

def consoantes_maiusculas(frase):
    lowercase = 'bcçdfghjklmnpqrstvwxyz'         #Mapa de consoantes minúsculos com 'ç'.
    uppercase = 'BCÇDFGHJKLMNPQRSTVWXYZ'         #Mapa de consoantes maiúsculas com 'Ç'.
    tabela = str.maketrans(lowercase, uppercase) #Cria a tabela de tradução de caracteres.
    return frase.translate(tabela)               #Retorna a frase com os caracteres convertidos.

Use:

>>>texto = 'Função que receba como entrada UMA frase e retorne a frase com todas as suas consoantes em maiúsculas'

>>>print(consoantes_maiusculas(texto)) 
FuNÇão Que ReCeBa CoMo eNTRaDa UMA FRaSe e ReToRNe a FRaSe CoM ToDaS aS SuaS CoNSoaNTeS eM MaiúSCuLaS

1


The problem is that upper returns another string. It does not change the original (by the way, this applies to all methods that "modify" the string - in Python, strings are immutable, and these methods actually always return another modified string).

So, you have to take the return and accumulate in another string:

def consoantes_maiusculas(frase):
    s = ''
    for caractere in frase:
        if caractere in 'bcdfghjklmnpqrstvxwyz':
            s += caractere.upper() # transforma em maiúscula
        else: # não é consoante minúscula, mantém como no original
            s += caractere
    return s

print(consoantes_maiusculas('abcdef')) # aBCDeF

Another alternative is to use join along with a Generator Expression:

def consoantes_maiusculas(frase):
    return ''.join(caractere.upper() if caractere in 'bcdfghjklmnpqrstvxwyz' else caractere for caractere in frase)

Inclusive, use join is the most recommended in this case, for being much more efficient than the first alternative.

  • Thanks!!!!!!!!!!

  • @If the answer solved your problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem

  • 1

    @hkotsubo, very calm at this time, I am also in the race :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.