Anti_vowel not working


#1

Not sure what isn't working here. It seems to be removing some of the vowels but not all of them. Please help!

def anti_vowel(c):
    newstring = ''
    for x in c:
        if x in "aeiouAEIOU":
            newstring = c.replace(x, '')        
    return newstring

The message I am getting is:

Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hey lk Wrds!" when it should return "Hy lk Wrds!".


#2

If you are insistent upon using the replace method, then some adjustment will be necessary.

You won't need a newstring variable, since all the work can be done in place on the inputs.

You will not have to iterate over the the inputs, only the vowels.

for x in 'aeiouAEIOU':
    c = c.replace(x, '')
return c

If no limit is set on replace then it is greedy by default. That means all instances of a needle in the haystack are removed at once.

We can also see there is no need for a conditional. A brute force approach might be doing more work than necessary, but needs no logic. It's purely search and destroy.

Now let's look at the mechanics of the following line, and why it doesn't work as expected...

replace is not an in-pace method. Nothing actually happens to the string. The method takes a snapshot and builds a new string that incorporates the replacement string.

In actual fact, the first replacement cycle of your code did take place, and was assigned to newstring, but the next replacement cycle overwrote it. Since the e is still in the haystack string, it is in the resulting string.

We make this an in-place operation by assigning back onto the same variable. We are replacing its value with each iteration.


#3

def anti_vowel(text):
    textlist = []
    textlist.append(text)
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    for letter in textlist:
        if letter == vowels:
            textlist.remove(letter)
            return textlist
        else:
            return textlist

I am trying to solve this problem using lists as I have learned that using lists requires less bytes for the most part than strings. But anyways, I wonder what's going wrong here? Thank you for any help in advance!


#4

That would bear further research. Strings are primitives with no data structure, but they are iterable. I cannot confirm one way or the other, but would suspect strings to use less memory than lists. Something to look up.

vowels = 'aeiouAEIOU'

Let text be "In the beginning was the void"

By the above line of code, textlist will be,

["In the beginning was the void"]

We cannot iterate it by letter. There is only one element.

There are other issues that will still need to be addressed. Simplify your code, and we can continue from there.


#5

Hm...

I tried messing with it in lists, but it just wasn't working.

I took the above code from bengkeller and tweaked it for the simplest answer.


#6

Don't give up. Lists are something that we need to get to know intimately, all the ins and outs. Keep fishing for a solution along those lines. Just because a simpler one can be had does not mean we shouldn't explore other possibilities. They might give us insight for future problems.

And, we need to discuss remove so you get it working right. Like I said, don't give up.


#7

# Used as another test. Frustrating, just returns the first letter of 'Hey look Words!'.
def anti_vowel(text):
    textlist = []
    vowels = 'aeiouAEIOU'
    for letter in text:
        textlist.append(letter)
        if 'e' in textlist:
            textlist.remove('e')
        return textlist  
    
    # I want to solve it with vowels in a list. 
    '''vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']'''
    
    # This returns a list of the text without removing anything
    '''for l in textlist:
        if l in vowels:
            textlist.remove(l)
            return str(textlist)
        return str(textlist)'''
        
    # This returns none
    '''while vowels in textlist:
        textlist.remove(vowels)
        return str(textlist)'''

#8

That line should be at the same block level as its declaration line.


#9

def anti_vowel(text):
    textlist = []
    for letter in text:
        textlist.append(letter)
        if 'a' in textlist:
            textlist.remove('a')
        if 'e' in textlist:
            textlist.remove('e')
        if 'i' in textlist:
            textlist.remove('i')
        if 'o' in textlist:
            textlist.remove('o')
        if 'u' in textlist:
            textlist.remove('u')
        if 'A' in textlist:
            textlist.remove('A')
        if 'E' in textlist:
            textlist.remove('E')
        if 'I' in textlist:
            textlist.remove('I')
        if 'O' in textlist:
            textlist.remove('O')
        if 'U' in textlist:
            textlist.remove('U')
    return str(textlist)

This gets it closer... I tried a whole bunch of functions to try and put it all together. Looked up a BUNCH and tried stuff from stackoverflow. Is there nothing that simply justs prints the ■■■■■■■ thing out as a string?


#10

def anti_vowel(text):
    textlist = []
    for letter in text:
        textlist.append(letter)
        if 'a' in textlist:
            textlist.remove('a')
        if 'e' in textlist:
            textlist.remove('e')
        if 'i' in textlist:
            textlist.remove('i')
        if 'o' in textlist:
            textlist.remove('o')
        if 'u' in textlist:
            textlist.remove('u')
        if 'A' in textlist:
            textlist.remove('A')
        if 'E' in textlist:
            textlist.remove('E')
        if 'I' in textlist:
            textlist.remove('I')
        if 'O' in textlist:
            textlist.remove('O')
        if 'U' in textlist:
            textlist.remove('U')
    return ''.join(textlist)

Well, I finally found the simplest way to do it and that is by the .join() function. Also, more complicated stuff that uses .join() here: https://stackoverflow.com/questions/19478804/fastest-way-to-convert-lists-into-plain-text. Thank you mtf for directing me. Though, surely there is a more elegant solution with lists? This is the only approach that I could come up with at my skill level.


#11

One approach is to build a new list of accepted letters.


newlist = []
for letter in text:
    if letter not in 'aeiouAEIOU':
        newlist.append(letter)
return ''.join(newlist)

Keep this problem on your radar so you come back to it after you've made more headway in the track.


#13

Thank you for your help and time mtf!!


#14

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