anti_vocal (works, but could it be simpler?)


#1

there has to be an simpler way to do this, this works but it seems to complicated:
def anti_vocal(c):
n=0
no_vocal=[]
while n!=len(c):
if c[0+n]=="a" or c[0+n]=="e" or c[0+n]=="i" or c[0+n]=="o" or c[0+n]=="u" or c[0+n]=="A" or c[0+n]=="E" or c[0+n]=="I" or c[0+n]=="O" or c[0+n]=="U":
no_vocal.append("")
else:
no_vocal.append(c[0+n])
n+=1
return ''.join(no_vocal)


#2

@lucarien Here some different ways to do the anti_vowels exercise I have commented on the principle or module being used to implement that method

you can use the iterative approach as follows

#=======================================================================
#================iterative approach====================================

def anti_vowel(text):
    #we are going to create a new text without
    #vowels
    final_text = ""
    
    '''now we loop through the given text'''
    for char in text:
        '''
        here we are checking if the char
        is a vowel
        remeber strings are iterables in python
        so when we write the little condition below
        each time the execution gets here it will
        convert the current char to lower case and
        go through 'aeiou' checking char against
        all letters in that string
        '''
        if char.lower() not in "aeiou":
            #if it does not match any of the letters in 
            #'aeiou' then add it to final_text
            final_text += char
    #finally return final_text
    return final_text
    
#call function
print (anti_vowel("The EARTH is a COOL PLACE to live"))

You can also use list comprehension as follows

#====================================================================
#====================using list comprehension========================
def anti_vowel(text):
    return "".join([x for  x in text if x.lower() not in "aeiou"])
    
#call function
print (anti_vowel("The EARTH is a COOL PLACE to live"))

You can also use lambdas follows

#======================================================================
#==================using lambdas(filter)===============================
def anti_vowel(text):
    return "".join(filter(lambda x : x.lower() not in "aeiou", text))

#call function
print (anti_vowel("The EARTH is a COOL PLACE to live"))

You can also use regular expressions as follows

#======================================================================
#==========================using regular expressions===================
def anti_vowel(text):
    import re
    return re.sub("[aeiouAEIOU]+", "", text)

#call function
print (anti_vowel("The EARTH is a COOL PLACE to live"))

You can also use bytes translation (mapping) as follows

#============================================================================
#==============================using bytes translation(mapping)==============
def anti_vowel(text):
    #for python3.x do
    #                          | |
    #                           ▼
    #return str(str.encode(text).translate(None, b'aeiouAEIOU'),'utf-8')
    
    return str(str.encode(text).translate(None, b'aeiouAEIOU'))

#call function
print (anti_vowel("The EARTH is a COOL PLACE to live"))

You can also test it here

Click here to post


#3

Thank you very much! I will take a closer look to this options