Average word length | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Average word length

You are in a college level English class, your professor wants you to write an essay, but you need to find out the average length of all the words you use. It is up to you to figure out how long each word is and to average it out. Task: Takes in a string, figure out the average length of all the words and return a number representing the average length. Remove all punctuation. Round up to the nearest whole number. here's my code isn't working import math a = input() b = a.replace(" ","") c = a.split() print(math.ceil(len(b)//len(c))) either another coDe is. a = input() b = a.replace(" ","") c = a.split() print(round(len(b)//len(c))) 1st code is getting matched with question case. and 2nd matching with first 2 answer case but still not getting approved. more cases are locked and I'm unable to check with that please help

14th May 2021, 6:21 PM
Swapnil Kamdi
Swapnil Kamdi - avatar
6 Answers
+ 2
At first you need to remove the punctuation. Using regex seems to be the easiest way. After that you create a list using .split(" ") And now you can count the length of each word using a loop, take the average (sum of length / number of words) and round it up with ceil()
14th May 2021, 6:58 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
First of all you are using floor division "//" Instead of simple division "/". The question also asks for removing all punctuation . In the following code i am not removing punctuations but checking for only alphabets and numbers . import math a = input() b=list(filter(lambda x:x.isalnum(),a)) c = a.split() print(math.ceil(len(b)/len(c))) You don't need round function , questions clearly says " round up " which is what math.ceil do. If you want to remove only punctuations instead of checking for alpha and num like i did , check the following question asked on stackoverflow. https://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string
14th May 2021, 6:52 PM
Abhay
Abhay - avatar
+ 1
text = input() text2 = text.replace(" ", "") print (len(list(text2))/len(text.split()))
15th Jul 2021, 7:51 AM
Kasymbekov Ulan Akylovich
Kasymbekov Ulan Akylovich - avatar
0
import math t = input() ch = "abcedfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" C = 0 for char in t: if char in ch: c += 1 w = len(t.split(" ")) print(math.ceil(c/w))
31st Jan 2023, 7:38 AM
Aravelli Prakash Rao
Aravelli Prakash Rao - avatar
0
An explanatory essay is a type of essay that asks the student to critically evaluate an idea, state it, and state the argument in a clear and concise manner. This can be done through comparison and contrast, which is the process of analyzing the cause and effect of an argument. Let's talk about an explanatory essay on "How does the Internet help students survive in college?
1st Aug 2023, 8:22 PM
Sara Jampen
Sara Jampen - avatar
0
[url=https://google.com/]google.com[/url] [url="https://google.com/"]google[/url] [url:google|https://google.com/] [url label="google"]https://google.com/"]google[/url] [link:https://google.com/ | google] [link:https://google.com/ google] [link url=https://google.com/]google[/link] [https://google.com/ google] [google.com](https://google.com/) [[https://google.com/ google]] [URL="https://google.com/"]google[/URL] [L=https://google.com/]google[/L] <a href=https://google.com/>google</a> "google":https://google.com/
30th Dec 2023, 9:00 AM
Albert Dexter
Albert Dexter - avatar