Practice Makes Perfect Anti Vowel


#1

What is wrong with my code? It threw the error that 'Hey Look Words' became 'Hy Lk Words' and didn't remove the last 'o'.

def anti_vowel(text):
    listy = []
    newtext = ''
    for chunk in text:
        listy.append(chunk)
    for section in listy:
        if (section in 'aeiouAEIOU'):
            listy.remove(section)
    for letter in listy:
        newtext = newtext + letter
    return newtext

#2

because remove is a difficult method. the problem is in look.

the problem is that, the moment we have is with the word look, the first o gets remove (then you have lok, but the second o of just shifted to the index of the first o, while the loop continued, essentially skipping the second o of look, then it finds another o in the word word, but .remove() will remove the first instance of o it can find (the one from lok)

Try appending constants to a new list, that is a easier method.


#3

You've got to consider what effect the remove method has. It causes all the following elements to move one step to fill the empty position in the list. Your loop would need to account for the moving elements, or you would need to avoid the situation entirely, such as by selecting what to add to another list instead of removing. Removing is also an expensive operation due to all the moving that has to be done.


#4

the problem with this code is the successive vowels in the word look

you loop over the string, then the first o (index: 1) of look will be removed. While this happens, the second o (index: 2) of look shifts to the position of the removed letter (so the second o of look will shift to index 1), while the loop continue to index 2, which thanks to the shift, now holds the letter k.

So, remove causes an index shift. Which means successive vowels don't get removed. In your case (hey look world) the o of world is found, but remove will remove the first instance of o it can find, which is in lok

Because of index shift, remove is a difficult approach of solving this problem. Try appending constants to a new list, that works much easier


#6

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