Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 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))
41 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.
+ 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
+ 5
https://code.sololearn.com/c3tFNP5i63Vp
+ 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))
+ 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), " ".
+ 3
Try using math.ceil() instead of round()
+ 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.
+ 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.
+ 3
text = input()
def longPromText():
countWord = len(text.split())
countLetters = len(text.replace(' ',''))
print(countLetters / countWord)
longPromText()
+ 2
Aymane Boukrouh has hit the nail on the head.
The task requires you to "round up" to the nearest whole number.
+ 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 🤷♂️.
+ 2
Ivan thank you, but it didn't fix the second problem.
+ 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
+ 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.
+ 2
words = input()
len_words= len(words) - words.count(" ")
tot = len_words / (words.count(" ")+1)
print(tot)
+ 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)
+ 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.
+ 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))
+ 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)
+ 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))))