Anti_vowel


#1

def anti_vowel(x):
	vowels = ["a","e","i","u","o","A","E","I","U","O"]
	novowels = []
	for item in x:
		novowels.append(item)
	for item in novowels:
		if item in vowels:
			novowels.remove(item)
	return "".join(novowels)

what is wrong with this code
error:
Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hy lk Words!" when it should return "Hy lk Wrds!".

and i was wondring what is the best function to remove strs you know what iam saying del or remove or ??


#2

From a list?
Removing an element from a list causes all the following elements to move one step to a lower index to fill up the index where a value was removed. If you did that manually you would quickly see that this is a lot of unnecessary work and you would instead take a different approach.

Either you didn't think through what removing does, or you want it to be removed differently. If the later, then implement that instead. Otherwise, refer to how you would do it manually and describe the same process in code.

Either way, you have to start by coming up with a series of steps that adds up to the desired result in a reasonable amount of time. If you haven't done that before writing your code, then what are you writing? Intent comes first.


#3

when i do it with str it come with error this is my problem so i made it a list plz tell me the best way to do it


#4

Strings can't be changed. But if they could you would still have the same problem of things moving when you remove from it. Get a piece of paper out and analyse how you would do this task. Get a firm idea of what it should be doing every step of the way.

You're asking for the best way to remove from a list, my reply is that removing from a list isn't a good idea in the first place because it does a lot more than you need.


#5

ok i will do this thanks


#6

I imagine that you would write down the text, and then immediately write down the same text below it, but without vowels. Note how you aren't removing anything - that would be awfully inefficient.


#7

okay okay dont worry


#8

If you removed, which you wouldn't, then you would need to modify your loop to not look ahead one step, because that is no longer where the next letter is. It moved.

If you just crossed it out, then you would need a second pass where you wrote it cleanly.

That's exactly the same actions as your code has to perform.


#9

Try strings. Initially I also tried using list but I realized it's easier with strings.


#10

hey thanks for help i passed and here is my clean code:

def anti_vowel(x):
    hey = ""
    for item in x:
        if item not in "aeiouAEIOU":
            hey += item
    return hey

#11

Yeah I'd fail that. :stuck_out_tongue:
Every time you add a new character, the existing string has to be re-created. That's the same kind of expensive as moving all the following characters each time one is removed, see the resemblance?

You'd get around it by appending to a list instead.

Other methods are using regex to substitute away what matches a vowel pattern, or using a generator expression which filters out vowels.

The list method you certainly can implement, the other two go like:

import re

def anti_vowel(text):
    return re.sub('[aoeuiAOEUI]', '', text)

def anti_vowel(text):
    return ''.join(chr for chr in text if chr not in 'aoeuiAOEUI')

There are plenty of other ways, for example splitting the text wherever there is a vowel, and then joining it:

import re

def anti_vowel(text):
    return ''.join(re.split('[aoeuiAOEUI]', text))

re.sub is probably the best one overall, the generator expression is also very nice though, it reads nearly like English. Splitting and joining is a little silly, doing more than required, but sure. It'll do the job.


#12

am i am english but i think iam understand :neutral_face:
is that saying that i was good at it or not :joy:


#13

learned new way to use join :smiley:
Thank you


#14

str.join is rather simple, it'll take any iterable that produces strings and mash them together with the specified separator (here an empty string)
There are a lot of ways to produce iterables, that's where the filtering logic will be, and then just feed the strings into str.join to create a single string all in one go (which is much desired as once created it can't be extended)


#15

It's correct. It's incredibly slow for large texts.
What you're doing is like this:

task: copy hello world
create all the following:

h
he
hel
hell
hello
hello 
hello w
hello wo
hello wor
hello worl
hello world

As opposed to just:

hello world

See how what you made is a pyramid? The amount of operations is n * n / 2, but the factor of 2 is negligible, so your method takes a quadratic amount of time, but should be linear

For small strings, sure, use concatenation. But if the text may be large, then use something that creates one string once all parts have been collected.


#16

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