Regarding average word length | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Regarding average word length

The challenge of writing code in esponse to calculating "Average word length" seems erroneous to me. The 2nd test in which there are 7 words and in total 31 letters must display 4 instead of 5. And when i ceil the answer other test cases becomes wrong. Please provide guidance. Will be appreciated. I am using python.

25th May 2020, 4:42 AM
Yasir Ali Khan
Yasir Ali Khan - avatar
4 Answers
+ 5
Read the text again and you'll find: "Round UP to the nearest whole number." Instead of using the round fumction you should use the ceil function don't forget to import it from math module first: from math import ceil
25th May 2020, 5:01 AM
Kevin ★
+ 2
Thanks Kevin Star Ceil function worked for me. Thanks 💞𝐒𝐨𝐦𝐲𝐚💞 Isaac Duah [Active!] for valuable insights. I appreciate you all for guidance
25th May 2020, 9:57 AM
Yasir Ali Khan
Yasir Ali Khan - avatar
+ 1
Check this out from math import ceil import re def average(text) : reg = re.compile(r'\w+') words = reg.findall(text) phrlen = sum(list(map(lambda x: len(x), words))) avg = phrlen / len(words) return ceil(avg) tx = input() avg = average(tx) print (avg)
25th May 2020, 5:12 AM
Isaac Duah [Active!]
Isaac Duah [Active!] - avatar
0
import re essay = input() word_list = essay.split() words = len(word_list) pat = r"\b\w+\b" tmp = re.findall(pat,essay) tmp = "".join(tmp) print(round(len(tmp)/words)) #print (str(len (tmp))+","+str(words )) 💞𝐒𝐨𝐦𝐲𝐚💞
25th May 2020, 4:47 AM
Yasir Ali Khan
Yasir Ali Khan - avatar