Anti-vowel, Recursive problem(SOLVED)



OK So the problem I had was a simple fix.

Originally, the problem I had was that I had a string index out of range error.

The solution: I added this to my code after line 16:

index_counter = len(text_without_vowels)-1

Then I called that new variable on the next line:

 return vowel_delete(index_counter, text_without_vowels)

This solved the string indexing issue. Essentially, every time the function detects a vowel, after the vowel is removed in the text_without_vowels variable, it resets the length of the text string, -1 to account for zero index.

This solved the string indexing issue.
I have found that the text.replace() function replaces ALL of the characters in the string, not just a character at that one index. I will continue to fine-tune this algorithm now.

Hey guys,

So I'm working on this problem recursively, but running into a problem. I'm wondering if it might have to do with memory, but I think it may be something else instead.

Running with what I have, "Hey You!" returns correctly as "Hy Y!"

However, when it passes "How does it feel? To be on your own?" it throws an IndexError: string index out of range

If you run what I have written so far, it throws the error on the first pass over a vowel in that long string (specifically, the 'o'). I don't think it should be doing that, but I can't figure out why.

Any advice?

On a side note, yes, I know recursive functions in python is a memory hog, but it's an extra challenge for me since I ma weak with recursive functions.

def anti_vowel(text):
    counter = len(text)-1
    if counter <= 1:
        return text
        return vowel_delete(counter, text)

def vowel_delete(index_counter, text):
    vowels = "aeiouAEIOU"
    if index_counter < 0:
        return text
    elif text[index_counter] in vowels:
        text_without_vowels = text.replace(text[index_counter], "")
        print text[index_counter], text
        return vowel_delete(index_counter-1, text_without_vowels)
        print index_counter
        return vowel_delete(index_counter-1, text)

print anti_vowel("Hey You!")
print anti_vowel("How does it feel? To be on your own?")


Hmm actually, I've done an anti-vowel recursive solution here. Let me find the link.

I didn't use a helper function as it wasn't necessary. Remember that the key to recursion are cases. It's pretty simple in this case. It has 3 cases,
If your text is empty, you return the text. (this is your base case and will tell the program when to stop)
If your letter is a vowel, skip it.
If your letter is not a vowel, return it and move on to next letter.

It's a little hard to wrap your head around (I struggled a little too) but it is important to understand how a recursive function is evaluated.

EDIT: found the link.


Yeah, that makes a lot of sense.


2 posts were split to a new topic: Scrabble Score, Recursive problem


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