Anti_vowel - Correct output but wrong answer


#1



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


I came up with a code that does exactly what is requested in the exercise and yet it doesn't seem to be accepted as a correct answer.

Oops! Your function ... returns Hy lk wrds! when it should return Hy lk wrds! LOL

I'd love to see other people's approach and solutions, thx :slight_smile:


"""function that removes all vowels from a string"""

word = raw_input("Enter a word: ")

def anti_vowel(word):
    w = str(word)
    w.lower()
    new_word = " "
    vowel_bin = " "
    
    for char in w:
        if char == "a" or char == "e" or char == "i" or char == "o" or char == "u":
            vowel_bin += char
        else:
            new_word += char
    return new_word
    
        
print anti_vowel(word)


#2

Your code is only removing lowercase vowels. Make sure you are removing both lowercase and uppercase vowels.


#3

Still getting the same Oops!

"""function that removes all vowels from a string"""

word = raw_input("Enter a word: ")

def anti_vowel(word):
    w = str(word)
    #w.lower()
    new_word = " "
    vowel_bin = " "
    
    for char in w:
        if char == "a" or char == "e" or char == "i" or char == "o" or char == "u"or char == "A" or char == "E" or char == "I" or char == "O" or char == "U":
            vowel_bin += char
        else:
            new_word += char
    return new_word
    
        
print anti_vowel(word)

#4

Instead of:

if char == "a" or char == "e" or char == "i" or char == "o" or char == "u"or char == "A" or char == "E" or char == "I" or char == "O" or char == "U":
    vowel_bin += char

Perhaps Try:

if char in "aeiouAEIOU":
    new_word = new_word.replace(char, "")

Also, your else statement, ... is it really necessary?


#5

or this way

def anti_vowel(text):
    vowels = {'a', 'e', 'o', 'u', 'i', 'A', 'E', 'O', 'U', 'I'}
    for char in vowels:
        text = text.replace(char, '')
    return text

#6

Why go through all that? Especially, when you could just do this:

def anti_vowel(text):
    for each in str(text):
        if each in "aeiouAEIOU":
            text = text.replace(each, "")
    return text

I mean, I don't understand why you would go through and make vowels a dictionary, especially when you could just use a simple string containing all the vowels. Seems a tad bit excessive, but then again, that's just me.


#7

It's simple exercise so maybe my solution is to excessive. I done it for few reasons:
1. it's cleaner, i have all vowels in second line of my function. It's easy to add 'y' and 'Y'.
2. It shall be faster cuz i use less control flow instructions.

But you're right, i can use string value instead dictionary.

I wrote few version this function and measured execute time

Functions:

def anti_vowel_stringValue(text):
    avc_start = time.clock()
    vowels = 'aeiouAEIOU'
    for char in vowels:
        text = text.replace(char, '')

    print('string value execute time: %s' % (time.clock() - avc_start))
   # return text



def anti_vowel_dictionary(text):
    avc_start = time.clock()
    vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    for char in vowels:
        text = text.replace(char, '')

    print('dictionary execute time: %s' % (time.clock() - avc_start))
   # return text

def anti_vowel_classic(text):
    avc_start = time.clock()
    for char in text:
        if char in 'aeiouAEIOU':
            text = text.replace(char, '')
    print('if in for loop execute time: %s ' % ( time.clock() - avc_start))
    #return text

As a function argument i used this article: http://www.wordyard.com/large-blocks-of-uninterrupted-text-a-talk-on-blogging-and-say-everything/

And this is my results:

string value execute time: 0.0008439999999999975
dictionary execute time: 0.0009270000000000007
if in for loop execute time: 0.251009

I use linux (kubuntu 15.10), it matter because documentation of clock function says:

Note: Not all systems can measure the true process time. On such systems (including Windows), clock usually measures the wall time since the program was started.

https://www.tutorialspoint.com/python/time_clock.htm


#8

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