0

Average Word Length

This is my code, str = input() word = len(list(str.split(' '))) alphanum = "" for i in str: if i.isalnum(): alphanum += i length = len(alphanum) avg = (length + word) // word print(avg) Test case: Can you not do that? My output: 4 Answer output: 3 Can any one please say what's wrong in my code , only one test case is going wrong other test cases are passed by this code ... can any one solve this

18th Jul 2021, 12:38 PM
Đì§hæ
13 odpowiedzi
+ 6
Abhay , i think it makes sense to check if char is alphanumeric or not, because the task description says: ▪︎< ...Remove all punctuation... >
18th Jul 2021, 4:06 PM
Lothar
Lothar - avatar
+ 6
Đì§hæ , as the code has some issues, i tried to modify it, by keeping as much as possible from your code. see also my comments in code. https://code.sololearn.com/c7RmUwn2Iqhm/?ref=app
18th Jul 2021, 4:44 PM
Lothar
Lothar - avatar
+ 4
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. Input Format: A string containing multiple words. Output Format: A number representing the average length of each word, rounded up to the nearest whole number. Sample Input: this phrase has multiple words Sample Output: 6 This is the question Abhay
19th Jul 2021, 12:39 AM
Đì§hæ
+ 3
Thanks Lothar , It worked and understood ur code better. Thanku 😃
19th Jul 2021, 1:04 AM
Đì§hæ
+ 3
And thanks to all helping me
19th Jul 2021, 1:04 AM
Đì§hæ
+ 2
avg=length/word . No need to use // which is floor division operator. Also i can't understand why you added word to length!! edit: you don't need to check if character is a alphanumeric as question never asked for that !!
18th Jul 2021, 1:07 PM
Abhay
Abhay - avatar
+ 2
Use ceil function and then convert it into rint function
18th Jul 2021, 3:14 PM
Atul [Inactive]
+ 1
Lothar the question that i am looking at has nothing written in description about "punctuation".
18th Jul 2021, 7:51 PM
Abhay
Abhay - avatar
+ 1
Lothar it also says nothing about rounding it up like you did in your code. And if you don't mind can i know why can't we use "str" as variable name ?
18th Jul 2021, 7:54 PM
Abhay
Abhay - avatar
+ 1
Looks like there are two Average Word Length , the one what op has doubt about and second one is here. https://www.sololearn.com/coach/1102?ref=app
18th Jul 2021, 8:01 PM
Abhay
Abhay - avatar
+ 1
Đì§hæ Here's a possible solution: a = input().translate(str.maketrans("", "", ",.?-!''")).split() print(int((k := sum(len(x) for x in a) / len(a)) + (k > int(k)))) # Hope this helps
20th Jul 2021, 8:33 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
This is word length checker in html:- https://code.sololearn.com/cytHoq2s7xNt/?ref=app
18th Oct 2021, 8:46 AM
Shubham Bhatia
Shubham Bhatia - avatar
+ 1
I had that issue also with the question mark.. so I used street sense. Lol import math sentence = input("") sen1 = sentence.replace("?", "") char_len = len(sen1.replace(" ", "")) word_list = sentence.split(" ") word_len = len(word_list) print(math.ceil(char_len/word_len))
20th Feb 2022, 6:09 PM
Obi-Okonkwo Chisom
Obi-Okonkwo Chisom - avatar