Anti_vowel -- I have no idea where to go after this


#1

I have no idea what I should write next.

Here is my code:

def anti_vowel(text):
_ new_text = text_
_ vowel = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, ‘U’]_
_ for c in range(0, len(text)):_
_ if c == vowel:_
_ new_text = new_text.remove©_
_ return “”.join(new_text)_
_ _
print anti_vowel(“Hello”)


#2

A string is iterable, just like a list.

vowels = "aeiouAEIOU"

If you have not yet learned about list.remove() then don’t use it. Most people who attempt to use this get it wrong. Think of something else that fits better with what has been taught so far.

Hint:

One approach would be to start with an empty string and then build it up with letters that are not in the vowel string.


#3

I found the concept of using list.remove() easier to understand. Here is the result of what I tried to do.

The loop only worked once, then it stopped. How should I fix it?

I also tried the another approach. Here it is.

At this point, I don’t think my problem came from I don’t understand list.remove() enough, but rather came from the way I try to use loop because both methods I tried had the same problem, they stopped working after the loop hit the first index that

in the first method: the first vowel index is removed
in second method: the first not-vowel index is returned

How should I fix it?


#4

Your first version:
You’re not accounting for elements moving when removing one of them. If you don’t mean for letters to move when you remove one of the others then list’s remove method doesn’t match what you mean and you will need to either use something else or change your plan.

list.remove does a lot of things that aren’t helpful here (wrong tool for the job) and is nothing but a whole lot of potential surprises

Your second version:
When you compare to a vowel in your if-statement, all other vowels are “allowed”, for example, if you have the letter ‘e’ and you compare to ‘a’, ‘e’ will be allowed because it isn’t equal to ‘a’, and it will be allowed multiple times even though it’s only one letter because you repeat this multiple times. Instead you need to compare to all vowels, and only after having done all those comparisons are you in a position to determine whether to include the current letter or not.

Both versions:
You’ve determined that there aren’t enough iterations happening, did you stop there? Sounds to me like a lead to follow up on, to start adding print statements in your code to find out when and where it exits. Your code very much says to exit at that time, there’s no subtle gotcha here.

Get yourself a better understanding of what everything you use does. Experiment with individual operations. If you figure out how to visit each letter, test whether to keep it, and to put letters together into a string - if you know how to do each of those then combining them is a rather trivial matter, and figuring out each one on their own is also a rather trivial matter.

-1
Why do your loops start at -1? On one hand it’s a good thing that you adjust as needed, on the other I think you adjusted the wrong thing when you should be visiting positions 0 up to the last one.
Much better yet is to not count at all, and to instead iterate over the letters (can’t really mess up “each letter”, but you can mess up the counting (and you do, except you exit before that happens))


#5

First rule of thumb: Don’t alter the list you are iterating.
Second rule of thumb: Start with a naive approach and get it to work, then look to make it as simple as possible. Don’t add more gears that are not needed.

The naive approach is to start with an empty string and a list of vowels (or better, a string).

result = ""
for letter in text:
    if letter not in "aeiouAEIOU":
        result += letter

When using list.remove() what happens to the list when an element is removed mid-loop? It gets shorter, and all the indexes to the right decrease by 1. That means one letter has slipped into the last position examined and it doesn’t get examined itself. Problem.


#6

Thank you for the clear explanation so I could see where I should fix clearer. I’ll try to fix it. I’ll let you know the result afterward.


#7

Thank you for the explanation. I admit that I have seen what you concerned somewhere on some post(s) here, but I didn’t give it much attention (which I should but I didn’t) to actually trying to fix it because part of me assume that the way I write the code in general will automatically fix it or it wouldn’t be a problem and I also admittedly can’t think of how to solve that problem. At least not at that moment and since no new idea come up in my mind about how to fix it at the time, I just jump right in to write whatever method that I could come up with. I’ll keep in mind not to overlook some small detail or some detail that I originally think was a small detail. I’ll try to be more careful from now on. No matter how careful I am, I can still easily overlook some important detail. Thanks again for answering.


#8

I was running out of idea and I feel like I stuck here for a week without moving forward so I checked with the solution. It turned out that my huge mistakes were not

  1. know that

for letter in text

that you gave me as a hint was actually meant that I can use it straight forward like that. I was so confused because I thought I have to write something else. Anything beside what you gave me. I thought what you gave me was meant to be just a hint. The idea of writing for letter in text actually means that I could write something similar to for letter in “Hello” with the replacement of the word “Hello” with a variable that contain a string.

I was so overwhelm by the for letter in range(start, stop) template or pattern for a beginner like me to remember or whatever else it calls that I forgot that for can be use without using the range pattern.

  1. My second mistake was I put “aeiouAEIOU” in a list and tried to use it with the variable text that contains string or even tried to converse text into a list by write new_text = list(text) which later became my problem.

I have learned my mistakes, fixed my code, made it work. Now it’s time for me to move on.

Thank you so much for answering my problem.


#9

for can be used on any iterable object (iterator or sequence) which is described in detail in the Python docs…

Built in Types

We can use for on strings, lists, dictionaries, tuples, sets, ranges, maps, filters, and anything else I may have missed (generators?) It takes a while to wrap one’s head around it when taken in huge chunks, but bite size and with lots of practice and experimentation will help sort through it all in time. Be patient with yourself and know that each granule is accumulating to a larger pile every day you keep working at it.


#10

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