Antivowel problem


#1

def anti_vowel(x):
g=[]
print x
for i in x:
g.append(i)
for i in g:
if i in "aeiouAEIOU":
g.remove(i)
f=""
for i in g:
f=f+i
return f
print anti_vowel("Hey look Words!")

this is my code for the problem and it seems that the function anti_vowel returns "Hy lk Words!" instead of "Hy lk Wrds!" . Where am i wrong?


#2

.remove() is really difficult approach, because you are dealing with index shifts.

the problem in this case is in look, when the first o is removed everything to the right of the removed character shifts to the left (because strings can't have empty indexes), while the loop progresses, causing successive vowels to skipped

its easier to use the opposite approach, append consonants to new list/string

if you really want to use remove, there are two ways to solve this, the easiest is to loop over x (the string) while removing from g (your list)


#3

Thanks alot , took me while to understand , but i understood


#5

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