+ 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))
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.
+ 14
Can someone upvote my question 😂 i need it to badge
+ 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))
+ 1
SUSHMITA K you forget to remove punctuations
0
n=input()
c=0
k=len(n.split())
for i in n:
if(i!=" "):
c=c+1
r=c/k
print(round(r))
0
What is wrong with my code.
0
I got it no need explanation
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
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))
0
viknesh vikky Thank you so much
0
text = input().strip()
words = text.split()
text = ''.join(words)
wlen,tlen = len(words),len(text)
print(tlen/wlen)
0
text = input()
count = 0
for i in text:
if i == " ":
continue
else:
count += 1
print(count/len(text.split()))
0
text = input()
text2 = text.replace(" ", "")
result = len(list(text2))/len(text.split())
print (result)