Average Word Length problem | SoloLearn: Learn to code for FREE!

+1

Average Word Length problem

I have a problem with the Code Coach challenge for Average Word Length. This is my code, which is working great for 4/5 tests. Not sure where I'm going wrong. from math import ceil #split input into list avg_string_list = input().split() # list comprehension to get average avg = sum(len(word) for word in avg_string_list)/ len(avg_string_list) #round up floats print(ceil(avg))

5/2/2020 1:06:26 AM

Curtis Dufour

6 Answers

New Answer

0

From your point, you not need ceil. Just add floor division. #split input into list avg_string_list = input().split() # list comprehension to get average avg = sum(len(word) for word in avg_string_list)// len(avg_string_list) if avg == 3: print (avg) else: print (avg+1)

+2

xLord , Curtis Dufour Forget it. got my mistake..Was counting punctuations as a character. Remove punctuations from your input string and it will work fine with *ceil* function. Here is my code: from math import ceil import string essay = str(input()) # Remove punctuations from essay string essay_new = essay.translate(str.maketrans('', '', string.punctuation)) # Split the essay into individual words words = essay_new.split() length_each_word = [] for i in range(len(words)): length_each_word.append(len(words[i])) total = sum(length_each_word) average = total/len(length_each_word) print(ceil(average))

+1

xLord ,A little help required. My solution to this problem was very similar to Curtis Dufour except that I didn’t use list comprehensions rather used simple for loop. I was also facing the same problem with “ceil” function and used floor division to complete the test. (thanks to you) However what I don’t get is ....why did you choose < avg == 3 > as the first condition ... Is it because of the explicit test case that was failing or is there any other reason ...Please explain What if there was a test case wuth avg value = 4 was present. Would this program still hold good«

0

Thanks! I always forget about floor division and how that works.

0

Solved :) https://code.sololearn.com/cZ0AGnbM9XEZ/?ref=app

0

I answered by his logic. I know is there a better code