Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite0
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.
4 Réponses
+ 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
+ 2
Thanks Kevin Star
Ceil function worked for me.
Thanks 💞𝐒𝐨𝐦𝐲𝐚💞 Isaac Duah [Active!] for valuable insights. I appreciate you all for guidance
+ 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)
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 ))
💞𝐒𝐨𝐦𝐲𝐚💞