Different and more efficient way of coding anti_vowel


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#

My code behaves correctly. I’m just wondering of a more efficient way to code it. Loops for example.



def anti_vowel(text):
    text = text.replace('A', '')
    text = text.replace('a', '')
    text = text.replace('e', '')
    text = text.replace('E', '')
    text = text.replace('i', '')
    text = text.replace('I', '')
    text = text.replace('o', '')
    text = text.replace('O', '')
    text = text.replace('U', '')
    text = text.replace('u', '')
    
    return text
print anti_vowel("Test") 



#2

There’s a few ways to do it. Here’s one way I have solved it.

You could easily do it without creating a another function like I did below, but I found it a bit more efficient to do it this way because I can change the text I’m looking at in the is_vowel function to lower-case and then only have 5 if statements instead of having if statements for upper-case letters, too.

48 PM


#3

Hi, this is the way I did it.
I am always trying to minimize the number of lines as much as possible. But i dont know if it’s the most efficient way In terms of performance. :slight_smile:

def anti_vowel(text):
    text_lower = text.lower()
    new_word = ""
    for x in range(len(text)):
        if not(text_lower[x] == 'a' or text_lower[x] == 'e' or text_lower[x] == 'i' or text_lower[x] == 'o' or text_lower[x] == 'u'):
            new_word += text[x]
    return new_word

#4

You can use a loop and make it shorter, just check the hint :wink:

it gives something like this:

def anti_vowel(text):
    txt = ""
    for i in text:
        if i not in "aeiouAEIOU":
            txt = txt+i
    return txt

#5

I hate checking hints , it feels like i didnt do it then… :smiley: Heh, i think i’ll start doing that ,thanks :slight_smile:


#6

I was answering the post author, who feel ok with asking on the forum, so i presume it’s ok to check the hint too, when you solved the problem your own way. :wink:


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.