+ 3

Trying to solve average word length in python data structure

I can't seem to find what is wrong with my code 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))

30th Mar 2021, 2:49 PM
Simisola Osinowo
Simisola Osinowo - avatar
6 Respuestas
+ 1
Thanks
31st Mar 2021, 8:43 AM
Simisola Osinowo
Simisola Osinowo - avatar
+ 1
from math import ceil txt = input().split() not_char = [".",",","?"] num_char = 0 for word in txt: for char in word: if char in not_char: continue num_char += 1 avg = num_char / len(txt) print (ceil(avg))
22nd Apr 2021, 9:06 AM
Ahmed Azami Hassani
Ahmed Azami Hassani - avatar
0
Thanks
30th Mar 2021, 3:45 PM
Simisola Osinowo
Simisola Osinowo - avatar
0
Here's the fixed code! https://code.sololearn.com/ceI3JO3H2DUs/?ref=app What's gone wrong with your code ? These two lines : words = float(len(re.sub('\W+',' ',string).split())) #do not require split method here letters = float(len(re.sub('\W+','',string))) # need \w+ rather than \W+ Also , no need to typecast len as it will always give you integral values. And do not have to worry about floating point division . 4/5 =.2 in python (already getting a float value )
30th Mar 2021, 4:20 PM
Hima
Hima - avatar
0
string = input() lis = string.split() length = 0 for i in lis: length += len(i) print(round(length/len(lis))) What's wrong with my code I can't pass two test case Anyone help me please?
1st Sep 2022, 11:20 AM
Joel Justin
Joel Justin - avatar