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

Average word length problem

2/5 didnt work My code phrase = input() lista = [] x = 0 suma = 0 average = 0 for i in range(len(phrase)): if(phrase[i] == " " and x!=1): lista.append(x) x = 0 else: x+=1 if(i == len(phrase)-1): lista.append(x) for z in range(len(lista)): suma += lista[z] average = suma / len(lista) if((average % 1) >= 0.5): print(int(average+0.5)) else: print(int(average))

1st Jan 2020, 9:34 PM
Bar Kro
Bar Kro - avatar
13 Answers
+ 11
If I recall correctly, some phrases end in ellipses and questions marks. They include punctuation of varying length (?? ... !!!!) Your code considers a word anything that ends with a whitespace. So the sentence “Good to see you!!” counts the word “you” as 5 instead of 3. Also if i could make two suggestions, try starting your code with: phrase = input().split(‘ ‘) This will eliminate the need for that first loop and think about using a regex to help you solve this. Good Luck and feel free to ask me more.
1st Jan 2020, 11:09 PM
Ivan
Ivan - avatar
+ 14
Can someone upvote my question 😂 i need it to badge
2nd Jan 2020, 10:19 AM
Bar Kro
Bar Kro - avatar
+ 2
n=input() c=0 k=len(n.split()) for i in n: if(i.isalpha()): c=c+1 r=c/k if(r==c//k): print(round(r)) else: print(round(r+1))
25th Feb 2020, 11:19 PM
karthik reddi
+ 1
SUSHMITA K you forget to remove punctuations
22nd Aug 2020, 4:25 PM
Karthik Madheti
Karthik Madheti - avatar
0
n=input() c=0 k=len(n.split()) for i in n: if(i!=" "): c=c+1 r=c/k print(round(r))
25th Feb 2020, 11:05 PM
karthik reddi
0
What is wrong with my code.
25th Feb 2020, 11:05 PM
karthik reddi
0
I got it no need explanation
25th Feb 2020, 11:20 PM
karthik reddi
0
import math string=str(input()) words = string.split() average = sum(len(wrd) for wrd in words) /len(words) print(round(average)) What is wrong with my code? Testcases 2&5 didn't work ... instead of round if I used math.ceil function it will cause only test case 1 failed.. others are passed. so pls anyone help me
9th May 2020, 7:01 PM
SUSHMITHA K
SUSHMITHA K - avatar
0
This is my solution with regular expressions. import re, math string = input() words = float(len(re.sub('\W+',' ',string).split())) letters = float(len(re.sub('\W+','',string))) print(math.ceil(letters/words))
14th Jul 2020, 2:09 PM
viknesh vikky
viknesh vikky - avatar
0
viknesh vikky Thank you so much
28th Jul 2020, 11:00 AM
SUSHMITHA K
SUSHMITHA K - avatar
0
text = input().strip() words = text.split() text = ''.join(words) wlen,tlen = len(words),len(text) print(tlen/wlen)
31st Mar 2021, 7:05 AM
Sachin Rajyaguru
Sachin Rajyaguru - avatar
0
text = input() count = 0 for i in text: if i == " ": continue else: count += 1 print(count/len(text.split()))
6th Jul 2021, 9:30 PM
Barış U.
Barış U. - avatar
0
text = input() text2 = text.replace(" ", "") result = len(list(text2))/len(text.split()) print (result)
15th Jul 2021, 7:49 AM
Kasymbekov Ulan Akylovich
Kasymbekov Ulan Akylovich - avatar