Skip to content

Instantly share code, notes, and snippets.

@liviocunha
Created March 10, 2021 13:45
Show Gist options
  • Save liviocunha/50fd4814062a156b0d683067020a63e9 to your computer and use it in GitHub Desktop.
Save liviocunha/50fd4814062a156b0d683067020a63e9 to your computer and use it in GitHub Desktop.
Desafio: Criar um método que conte vogais e consoantes numa frase
class Word(object):
def __init__(self):
self.vowels = ["A", "a", "á", "ã", "â", "à", "E", "e", "é", "ê", "I", "i", "í", "O", "o", "ó", "õ", "ô", "U", "u", "ú", "Y", "y"]
self.others_char = [" ", ",", ".", ";", "!", "?", "-", "_", "'", '"', "*", "+", "/"]
def vowels_count(self, phrase):
''' Percorre a frase e conta somente as vogais. '''
count_vowels = 0
for char in phrase:
if char in self.vowels:
count_vowels += 1
return count_vowels
def vowels_consonants_count(self, phrase):
''' Percorre a frase e conta as vogais e consoantes. '''
count_vowels = 0
count_consonants = 0
for char in phrase:
if char not in self.others_char:
if char in self.vowels:
count_vowels += 1
else:
count_consonants += 1
return (count_vowels, count_consonants)
phrase_01 = Word()
string_phrase_01 = "Ada Lovelace criou o primeiro algoritmo para ser interpretado por máquinas."
phrase_02 = Word()
string_phrase_02 = "Irmã Mary Kenneth Keller viu os computadores como uma ferramenta educacional e criou a linguagem Basic."
# Conta somente as vogais
print(f"FRASE 01 -> Vogais: {phrase_01.vowels_count(string_phrase_01)}")
print(f"FRASE 02 -> Vogais: {phrase_02.vowels_count(string_phrase_02)}")
print("\n")
# Conta vogais e consoantes
counts_phrase_01 = phrase_01.vowels_consonants_count(string_phrase_01)
counts_phrase_02 = phrase_02.vowels_consonants_count(string_phrase_02)
print(f"FRASE 01 -> Vogais: {counts_phrase_01[0]} | Consoantes: {counts_phrase_01[1]}")
print(f"FRASE 02 -> Vogais: {counts_phrase_02[0]} | Consoantes: {counts_phrase_02[1]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment