+ 1
[SOLVED] Average Word Length Challenge
What did i miss? It fullfilled the main test cases but it did not pass 2 hidden test and i cannot think of those cases. Could anyone enlighten me what are those possible cases? here's the challenge: https://www.sololearn.com/coach/73?ref=app and here's my code: https://sololearn.com/compiler-playground/ckBzw0IW1VCz/?ref=app
9 odpowiedzi
+ 2
Thera is one problem: characters like "*,.-?" Are not letters. This means that the answer for "there is a problem..................." is invorrect given that "." Is counted as a letter, but is not
+ 3
SeaWater ,
the conditional can be done a bit simpler, because we need to count only all letters by using isalpha():
...
def countchar(stringinput):
charcounter = 0
for letter in stringinput:
#if not letter.isalpha() or letter == " ":
if letter.isalpha():
#charcounter += 0
charcounter += 1
#else:
#charcounter += 1
return charcounter
...
this should pass all test cases.
+ 3
SeaWater ,
isalpha() returns True for all letters, but not for punctuation and not for spaces.
+ 1
import math
sentence = input("")
sen1 = sentence.replace("?", "")
char_len = len(sen1.replace(" ", ""))
word_list = sentence.split(" ")
word_len = len(word_list)
print(math.ceil(char_len/word_len))
0
and the funny thing is, if i add one more validation using isalpha() into the loops in countchar function now the results was reverse, the hidden test cases were all pass but the main test cases didn't
0
AiVolt,
You're right that did the trick after adding the isalpha method. But i did find some inconsistencies as:
if not letter.isalpha() or letter == " "
is not the same as
if letter == " " or letter.isalpha() == False
why?
i did the latter first but it still counting the non alphabet character.
(edit: nevermind just ignore this, maybe i did something wrong back there, it works just the same after i tried it again.)
0
Lothar,
It is stated that spaces can't be counted though? Are you implying that isalpha method not only counted special characters but also spaces?
(edit: wow, it did. Just tried it a moment ago. )
0
Lothar,
Man, isalpha method is really that convenient huh? :) Can't imagine we must replace all those character and spaces manually with traditional "" if that method did not exist.
0
<happy mother's dayp/>