Home > Back-end >  How do I fix this code so that it doesn't take out vowels that are consecutive of one another?
How do I fix this code so that it doesn't take out vowels that are consecutive of one another?

Time:10-28

Hey guys so I'm writing a code for text message abbreviations and these are the following criteria:

  • Spaces are maintained, and each word is encoded individually. A word is a consecutive string of alphabetic characters.
  • If the word is composed only of vowels, it is written exactly as in the original message.
  • If the word has at least one consonant, write only the consonants that do not have another consonant immediately before them. Do not write any vowels.
  • The letters considered vowels in these rules are 'a', 'e', 'i', 'o' and 'u'. All other letters are considered consonants.

I have written a code and checked it but it is failing for the condition where if the word is composed only of vowels, it is written exactly as in the original message. My code currently is taking out all of the vowels. Like for this example, "aeiou bcdfghjklmnpqrstvwxyz" the code should return "aeiou b" I tried using another helper function to determine when a word is all vowels but it isn't working. Any suggestions on how to implement something that could make this work? Thanks!

def Vowel(x):
    vowels = "a" "e" "i" "o" "u"
    value = 0
    for ch in x:
        if x == vowels:
            value = value   1
        return value

def isVowel(phrase):
    vowel = "a" "e" "i" "o" "u"
    value = 0
    for ch in phrase:
        if ch in vowel:
            value = value   1
    return value

def noVowel(ch):
    vowel = "a" "e" "i" "o" "u"
    value = 0
    for i in ch:
        if ch not in vowel:
            value = value   1
    return value

def transform(word):
    before = 'a'
    answer = ""
    for ch in word:
        if Vowel(ch):
            answer  = ch
        if noVowel(ch) and isVowel(before):
            answer  = ch
        before = ch
    return answer

def getMessage(original):
    trans = ""
    for word in original.split():
        trans = trans   " "   transform(word)
        trans = trans.strip()
    return trans

if __name__ == '__main__':
    print(getMessage("aeiou b"))

CodePudding user response:

Your three Vowel-functions are all using a disfunctional for-loop and are highly redundant. Also Vowel() would always return 0, as x may never be a tuple of five vowels.

You would need only one function that just returns True if the character is a vowel and False if it is not. Then, you can use this function in the if-blocks in transform:

def Vowel(x):
    vowels = ["a","e","i","o","u"]
    return x.lower() in vowels ## True if x is a vowel (upper or lower case)

def transform(word):
    before = 'a'
    answer = ""
    for ch in word:
        if Vowel(ch):
            answer  = ch
        if not Vowel(ch) and Vowel(before):
            answer  = ch
        before = ch
    return answer

CodePudding user response:

You have not identified what to do with words that have both consonants and vowels...if you provide a few more example inputs and outputs, I might decide to change my code, but this is what I have come up with so far.

I have used regex:
(\b[aeiou]*\b) : finds vowel only words
([bcdfghjklmnpqrstvwxyz]*) : looks for groups of consonants.

The for-loop : helps to return the vowel-only words, or the first letter of the consonant groups.

import re

strings_to_test = ["aeiou bcdfghjklmnpqrstvwxyz ae hjgfd a",
                   "gg jjjjopppddjk eaf"]

pattern = re.compile(r"(\b[aeiou]*\b)|([bcdfghjklmnpqrstvwxyz]*)")

for line in strings_to_test:
    results = []
    match_list = pattern.findall(line)
    
    for m in match_list:
        if m[0]:
            # word of vowels found
            results.append(m[0])
        elif m[1]:
            #consonants found
            results.append(m[1][0])
            
    print(results)

OUTPUT:

['aeiou', 'b', 'ae', 'h', 'a']
['g', 'j', 'p', 'f']
  • Related