Reputation: 147
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?
Upvotes: 3
Views: 1741
Reputation: 18670
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:
(O(1))
find_vowel_index
in bebis
, it could be structured better.Upvotes: 0
Reputation: 129497
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
Upvotes: 4
Reputation: 20339
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.
Upvotes: 0