+ 11

About the Average Word Length problem

I am struggling with this problem since yesterday. I did what i can do, but I couldn't get 5/5. No matter what i did, I always get 3/5. can you see what I'm missing in this code? # as a remainder, the average word length problem is: you have an input with multiple words, and the output should be the average of letters per word rounded to the nearest whole number. And Here's the code: import string punc= string.punctuation test= input() phrase=" " count_letter =0 count_word =0 for i in range(len(test)): phrase +=str(test[i]) if phrase[i] == " ": count_word +=1 if phrase[i] != " " and (phrase[i] not in punc): # not counting spaces and punctuations count_letter +=1 print(round((count_letter +1)/count_word))

3rd Jan 2020, 12:41 AM
AbdullaH
AbdullaH - avatar
40 Answers
+ 3
Try using: (count_letter//count_word)+1 Also, im really glad i read your question. i had no idea there was a string library. I used regex to solve this.
3rd Jan 2020, 3:45 AM
Ivan
Ivan - avatar
+ 6
Your code adapted. I bypassed the word =" ", and iterated directly from the input. Also a minor change to word_count = This now works. Thanks heaps for sharing your interesting concept https://code.sololearn.com/cdm958D8hhbU/?ref=app
3rd Jan 2020, 4:12 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 5
https://code.sololearn.com/c3tFNP5i63Vp
3rd Jan 2020, 5:43 AM
David Ashton
David Ashton - avatar
+ 4
import string punc= string.punctuation test= input() phrase=" " count_letter =0 count_word =0 for i in range(len(test)): phrase +=str(test[i]) print("character: " + phrase[i] + " ~ ") if phrase[i] == " ": count_word +=1 print("Adding a Word") elif phrase[i] != " " and (phrase[i] not in punc): # not counting spaces and punctuations count_letter +=1 print("Adding a Letter") print(round(((count_letter)//count_word)+1))
3rd Jan 2020, 4:11 AM
Ivan
Ivan - avatar
+ 4
I am also playing with the code and getting some discrepancies when I introduce an extra " " space into the sentence. The code counts an extra word each time it sees chr(32), " ".
3rd Jan 2020, 6:13 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Try using math.ceil() instead of round()
3rd Jan 2020, 12:55 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
also you can’t use the code i gave you for rounding because if you floor a number like 3.0 it becomes 3.0. And then you add 1, it becomes 4. math.ceil() is the right choice. But thats not the issue. ——————-Spoiler ahead: The issue starts all the way at the beginning of your code with the line phrase = “ “ because then you do: phrase += str(test[i]) That means phrase is now “ word” Which means phrase[i] = “ “ And your if statement says if phrase[i] = “ “ then add a word!!! To solve your issue, all you have to do is is change: phrase = “ “ to: phrase = “” And you’re done. Lol, coding can be so cruel. All this over a damn whitespace.
3rd Jan 2020, 4:29 AM
Ivan
Ivan - avatar
+ 3
you have to remove the floor and replace it with ceil and get rid of the + 1 right now you are doing this: 13//3 = 3.0 + 1 + 1 = 5 i just copied yours and removed that and it worked. edit: nope. i was using the old one you wrote. Davids code is good for learning more methods, but i think you should try to make your code work. It’s almost there. I’ll post mine as well above yours. Your missing two conditional statements.
3rd Jan 2020, 5:50 AM
Ivan
Ivan - avatar
+ 3
text = input() def longPromText(): countWord = len(text.split()) countLetters = len(text.replace(' ','')) print(countLetters / countWord) longPromText()
23rd Apr 2021, 7:06 PM
Elias Marino
Elias Marino - avatar
+ 2
Aymane Boukrouh has hit the nail on the head. The task requires you to "round up" to the nearest whole number.
3rd Jan 2020, 2:01 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
It did surely made a difference. I'm now getting 4/5, but now a new problem came out. The (count_letter) part, when doing ((count_letter +1)/count_word) test 5 fails. And when instead doing (count_letter/count_word) test 1 fails 🤷‍♂️.
3rd Jan 2020, 2:04 AM
AbdullaH
AbdullaH - avatar
+ 2
Ivan thank you, but it didn't fix the second problem.
3rd Jan 2020, 3:55 AM
AbdullaH
AbdullaH - avatar
+ 2
To round up to the next number you can use ceil() in the math module. from math import ceil print(ceil(4.1)) # output: 5
3rd Jan 2020, 5:24 AM
David Ashton
David Ashton - avatar
+ 2
Ivan Thanks, I don't know if it does work because I copied the code David Ashton wrote to test it and it worked. But it is a completely different approach.
3rd Jan 2020, 2:19 PM
AbdullaH
AbdullaH - avatar
+ 2
words = input() len_words= len(words) - words.count(" ") tot = len_words / (words.count(" ")+1) print(tot)
28th Aug 2021, 9:29 PM
Hamdambek🖥️🎧
Hamdambek🖥️🎧 - avatar
+ 2
Try my code for #Python import string import math punc= string.punctuation test= input() phrase= "" count_letter =-1 count_word =1 for i in range(len(test)): phrase +=str(test[i]) if phrase[i] == " ": count_word +=1 if phrase[i] != " " and (phrase[i] not in punc): count_letter +=1 print(math.ceil((count_letter//count_word)) +1)
1st Feb 2022, 7:38 PM
Avrian Shandy
Avrian Shandy - avatar
+ 1
ok so i figured out your problem. First use the code that shows you the failing test case like the one i gave you. Here’s a copy of your code that i changed a little. Run it on your end and you will see what is happening that is causing you to round incorrectly.
3rd Jan 2020, 4:10 AM
Ivan
Ivan - avatar
+ 1
'''#My Answer: import re,math as m x = list(map(lambda y: len(y), ''.join(re.findall('[A-Za-z ]',input())).split(' '))) print(m.ceil((sum(x)/len(x)))) ''' import string import math punc= string.punctuation test= input() phrase= "" count_letter =0 count_word =0 for i in range(len(test)): phrase +=str(test[i]) print(phrase + "-----------") if phrase[i] == " " or phrase[i] in punc: count_word +=1 print("Add a word") if phrase[i] != " " and (phrase[i] not in punc): count_letter +=1 print("Add a letter") # you need to check to see if the before a punctuation there is another punctuation. like “Hello there...” print(math.ceil(count_letter/count_word))
3rd Jan 2020, 6:08 AM
Ivan
Ivan - avatar
+ 1
Try my code: Average word length: import string import math punc= string.punctuation test= input() phrase= "" count_letter =-1 count_word =1 for i in range(len(test)): phrase +=str(test[i]) if phrase[i] == " ": count_word +=1 if phrase[i] != " " and (phrase[i] not in punc): count_letter +=1 print(math.ceil((count_letter//count_word)) +1)
19th Apr 2020, 12:40 PM
|| AK ||
|| AK || - avatar
+ 1
word = input() str = word.split() import re, math mystr = re.sub(r"[^A-Za-z]", "", word) print (int(math.ceil(len(mystr)/len(str))))
27th Apr 2020, 7:06 PM
Мансур Валиев
Мансур Валиев - avatar