8/15 - what am i doing wrong here?


#1

def anti_vowel(text):
x=""
l=len(text)

while l>0:
    for char in text:
        if c in char == "a":
            del "a"
        if c in char == "A":
            del "A"
        if c in char == "e":
            del "e"
        if c in char == "E":
            del "E"
        if c in char == "i":
            del "i"
        if c in char == "I":
            del "I"
        if c in char == "o":
            del "o"
        if c in char == "O":
            del "O"
        if c in char == "u":
            del "u"
        if c in char == "U":
            del "U"
return text

#2

You can do it like this,

EXAMPLE:

def anti_vowel(text):
    word = ''
    for i in text:
        if i.lower() not in 'aeiou':
            word += i
    return word

OR

def anti_vowel(word):
    return "".join([letter for letter in word if letter not in 'aeiou'])

#3
def anti_vowel(text):
    word=""
    for i in text:
        if i not in 'aeiouAEIOU':
            word += i  
    return word

#4

The two solutions presented here essentially amount to selectively rebuilding the string with individual characters that satisfy a logical "if it is not a vowel, use it." Can you solve this problem by selectively removing individual characters, like the code in the first post is trying to do, or do you need to add the characters to a new string?


#5

ferretey as u wanted :
def anti_vowel(text):
i = len(text)-1
while i >= 0:
if text[i] in "aeiouAEIOU":
text = text.replace(text[i], "" )
i = i-1
else:
i = i-1
return text
print anti_vowel('network')


#6

my code for the future generations

def anti_vowel(text):
for letter in text:
if letter in "aeiouAEIOU":
text = text.replace(letter,"")
return text

#7

I did something similar, and yours looks cleaner and makes more sense, but would one run faster than the other? Which is more efficient?

def anti_vowel(text):
    new_text = []
    for i in text:
        if i not in "aeiouAEIOU":
            new_text.append(i)
    return "".join(new_text)

#8

@ammonymous

Well lets check it out,

TLDR:
Even after 1,000,000 calls to each one there is only a second difference of 0.304 of a sec. Or per each function call 3.0399999999999997e-07 of a sec. Not much to even matter at all. But on calls the first code bit is 1/2 of the second code.

EDIT:
I should also mention that if the string is larger the second code bit will see drastic speed drops.

CODE

import cProfile

Then let's put one function in now

def anti_vowel(word):
    return "".join([letter for letter in word if letter not in 'aeiou'])

Then let's create a function to rerun the code 1,000,000 times,

def repeat_code(code, string):
    for x in range(1000000):
        eval("%s(%s)" % (code, string))

Then let's use cProfile to run the code and time it.

cProfile.run("repeat_code('anti_vowel', 'string')")

Then we will test the second code,

def anti_vowel(text):
    new_text = []
    for i in text:
        if i not in "aeiouAEIOU":
            new_text.append(i)
    return "".join(new_text)

Ok

FIRST CODE:

         4000004 function calls in 24.834 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1000000    1.370    0.000    3.203    0.000 <pyshell#112>:1(anti_vowel)
  1000000    1.508    0.000    1.508    0.000 <pyshell#112>:2(<listcomp>)
        1    1.283    1.283   25.619   25.619 <pyshell#120>:1(repeat_code)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
  1000000   20.348    0.000   24.336    0.000 {built-in method eval}
        1    0.000    0.000   25.620   25.620 {built-in method exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  1000000    0.325    0.000    0.325    0.000 {method 'join' of 'str' objects}

SECOND CODE:

         8000004 function calls in 25.138 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    1.303    1.303   26.033   26.033 <pyshell#120>:1(repeat_code)
  1000000    2.689    0.000    3.757    0.000 <pyshell#124>:1(anti_vowel)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
  1000000   20.077    0.000   24.730    0.000 {built-in method eval}
        1    0.000    0.000   26.033   26.033 {built-in method exec}
  5000000    0.765    0.000    0.765    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  1000000    0.304    0.000    0.304    0.000 {method 'join' of 'str' objects}

#9

@ammonymous

I did a test of a little longer string because I was thinking that the difference would become a lot larger the longer the string is.

As you will see below the function call count on your code exponential increases based on the length of the string. the same is not true of the code I have created.

Here is the new Code:
Mine:

import cProfile

stringz = 'This string is going to be a little longer this time to really test the difference!'


def anti_vowel(word):
    return "".join([letter for letter in word if letter not in 'aeiou'])

def repeat_code(code, string):
    for x in range(500000):
        eval("%s(%s)" % (code, string))


cProfile.run("repeat_code('anti_vowel', 'stringz')")

Yours

def anti_vowel(text):
    new_text = []
    for i in text:
        if i not in "aeiouAEIOU":
            new_text.append(i)
    return "".join(new_text)

Now the results for our new string!

Results
Mine:

2000004 function calls in 16.662 seconds @500,000 Calls

Yours

31000004 function calls in 22.715 seconds @500,000 Calls

1200 Character String

stringz = "CTXABNPSHKJTPOCCMXCJEJDGMEMSSXUWXTWJTTBEEHVFYIUQ" \
          "QSPBZSJJEPMTOVNRHCIDZMFPNSVIOVEMFOOJMAVKQZLJUZCE" \
          "YREOULNMRRSGEPCCWSONPQDJEEEFPAQRODMBPANTPVEUXYVY" \
          "UITZEMMPQGDEMXUUMEUGGWYTIIUVHWTLPRNWCWNMKERDUTDF" \
          "QELCLCTLJQRMRVYKOLVDSBRCTSKJOLZNKKMWASMQUVYENJFF" \
          "MXECZDRIUWJQVFKKFRSFUESXTBJXGEOTVDBFXLEDFDSLPRHB" \
          "HJCFXLKRHKQMJAAOEPMISWGAGOZZRZBPJFPXDHTXFTLREXAQ" \
          "XCQXNEEULQPOMODJILDRLLEQXYSTKOYOQLKIBPECGTOCIEIP" \
          "SLFQXSCQSBLXMKRCADDXCSMHGZAAGMMZYGHZHXJUXCTWXRZE" \
          "TAIDJOVGMAQNWVBXPEQHINXAKARGFYCCOUAZOBGQMHSGSOEJ" \
          "KMJHSMYWHDYPMVCQTJCNWDTFTFYRVZHDALMSYWNDXJHHHJLG" \
          "FMFPVAROPDJAVPRYNHMIBHEWJCHFNPQKTCZPIARQFESJYOPS" \
          "GLPWVRKCIQXWTBCGTEBWFWVGWRESMPMWXPYTLBGWPHEKEEVE" \
          "RAMIHWWFKSIVHKHGAIWOVFHPYUAEDTIVYFEZEUGWMBFNOIIA" \
          "JETAQAKTPRBQSOQXOQWYFKEEPKHLEAWTBXNLZXUSSZAKEAUG" \
          "IKQTYREUZNFKMERBKUXMCMPFYTVHXCWOGAQXTTEAREZEJJLH" \
          "SQCBEQQOMPCNNVSULBGHAOJWOEVLJGCJUQVHHFQIVLVLRFMT" \
          "EJZVTCVBRFYQRSOYBPOAQLUIEAMIVVRAJTESMTTPLBKDJQQP" \
          "WXJRSFGKNZTBIGNAMKEQWXCPAMWWHIIDQCLWCPFWVFQVZGWC" \
          "WYLDDHZXIHWTRADNXCESONBFOEJACLMDNUWTUGRFSISKJYSP" \
          "CCFPIAOFTUUCGJFBRYSINFXWUYTNVXQZRHNLKWWWNAGFUPQK" \
          "LETXGPAHRTAWCRTQMSIEKKXBLJNRACCHHNKLVMPBSNKRDQIT" \
          "IXNZMSRIYUVEGRYNKBHBAZCHLQMUEXBKPPWOUXUSPSVMDBVR" \
          "VPMVJWBRNOBCPQOMQACGVMLBHYOSJWARJZHYNEESRIBPGBGY" \
          "VVHZAFYVXKQAQAGLZVAXFPLASBJBTKHFMZGIFIRAQBTBSFGN"

Mine

1000004 function calls in 39.968 seconds @250,000 Count

Yours

240750004 function calls in 86.783 seconds @250,000 Count

#10

Have you checked your code before posting it? When I tried it, it returns a message saying Your function fails on anti_vowel("Hey look Words!"). It returns " H" when it should return "Hy lk Wrds!".


#11

@ronitdanti

It must be on your end because it does work. If you read the code it does what it says on the tin. I would use a IDE or reload the web page that you are using to run the code on.


#12

Sorry, I realised that I was using your old code.


#13

i'm little confused why 'replace' method not change color such a del method? can you tell me ?