3

Been sitting on this minor problem a few days now, I don't know if I have it all wrong or just missed out on something.

The Objective: From each word in a sentence - Find the first vowel, remove the letters after that vowel from the word and multiply the remaining letters by 3.

The Example: If I have the sentence: "Hello World" the wanted output should be "HeHeHe WoWoWo".

My Code:

def bebis(inrad):
 utrad = ""
 inrad = inrad.split()
 for tkn in inrad:
 for tkn1 in tkn: #Eftersom tkn ar ordlista nu.
 if tkn1 in vokaler:
 count = len(tkn1)
 utrad += tkn1
 elif tkn1 in konsonanter:
 utrad += tkn1
 return utrad[:count+1]*3
print("Bebisspraket:",bebis(inrad))

My Thoughts: I split the sentence into a lists of words using split(). Then I use two for loops, one that should go through each word and the other one that should go through each letter in every word. If it finds a vowel, count where it is and then return the letters to the first vowel of the word.

My Problem: The output only gives me the first WORD in a sentence and breaks from there. So "Hello World" yields "HeHeHe" leaving me super frustrated. Why does it not go through the rest of the sentence?

asked Sep 22, 2012 at 13:45

3 Answers 3

4

How about something like this:

import re
def bebis_word(word):
 first_vowel = re.search("[aeiou]", word, re.IGNORECASE)
 if first_vowel:
 return word[0:first_vowel.start() + 1] * 3
 else:
 return '' 
def bebis(sentence):
 words = [bebis_word(word) for word in sentence.split()]
 return " ".join(words)
print bebis("Hello World")

Output:

HeHeHe WoWoWo

answered Sep 22, 2012 at 14:06
Sign up to request clarification or add additional context in comments.

1 Comment

Nitpicky: It's probably a good idea to compile the regex with re.compile and reuse the compiled regex.
0

Your approach seems correct (splitting the sentence into words and iterating on the words to find the first vowel).

The problem is that your tkn1 variable is a letter, so len(tkn1) is always 1, so count=1

Here's a potential solution:

def bebis(inrad):
 utrad = ""
 inrad = inrad.split()
 # Loop on words 
 for tkn in inrad:
 # Loop on letters in the word
 for (i,tkn1) in enumerate(tkn): #Eftersom tkn ar ordlista nu.
 if tkn1 in vokaler:
 utrad += tkn[:i+1] * 3
 break
 utrad += " "
 return utrad

Here, we use the enumerate function that will give a list of tuples (index,letter). Once we tested that the current letter is a vowel, we take the first letters of the word (tkn[:i+1]), repeat them three times, store them in utrad and move to the next word (with the break statement that leaves the current loop). We just have to add an extra space between words.

answered Sep 22, 2012 at 13:59

Comments

0

Just as an alternative to the regular expression approach, I did:

def find_vowel_index(word):
 vows = set(["a", "e", "i", "o", "u"])
 for i, letter in enumerate(word):
 if letter in vows:
 return i
 return -1 
def bebis(s, repeat=3):
 return " ".join([word[0:find_vowel_index(word)+1] * repeat for word in s.split() if find_vowel_index(word) >= 0])

Thoughts:

  • I did a set for the vowels as the "in" test on a set is a constant time operation (O(1))
  • added an optional repeat argument to bebis, so that if you want the word to repeat some number of times other than 3 it's easy to do so.
  • I don't like the multiple calls to find_vowel_index in bebis, it could be structured better.
  • a trade-off over the regex version is that this will get slower as words get longer (and have vowels "deeper" in the word). OTOH, I'd guess that on short words the overhead of the regex might be a bit costly.
answered Sep 22, 2012 at 14:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.