Average word length problem | SoloLearn: Learn to code for FREE!

+13

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))

1/1/2020 9:34:45 PM

Bar Kro

10 Answers

New Answer

+10

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.

+12

Can someone upvote my question 😂 i need it to badge

+1

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