Regarding average word length | SoloLearn: Learn to code for FREE!


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.

5/25/2020 4:42:39 AM

Yasir Ali Khan

6 Answers

New Answer


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


Thanks Kevin Star Ceil function worked for me. Thanks 💞𝐒𝐨𝐦𝐲𝐚💞 Isaac Duah [Active!] for valuable insights. I appreciate you all for guidance


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)


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 )) 💞𝐒𝐨𝐦𝐲𝐚💞