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
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... >
+ 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
+ 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
+ 3
Thanks Lothar ,
It worked and understood ur code better.
Thanku 😃
+ 3
And thanks to all helping me
+ 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 !!
+ 2
Use ceil function and then convert it into rint function
+ 1
Lothar the question that i am looking at has nothing written in description about "punctuation".
+ 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 ?
+ 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
+ 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
+ 1
This is word length checker in html:-
https://code.sololearn.com/cytHoq2s7xNt/?ref=app
+ 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))