[SOLVED] Average Word Length Challenge | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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

10th May 2024, 2:22 PM
SeaWater
SeaWater - avatar
10 Réponses
+ 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
10th May 2024, 2:36 PM
AiVolt
AiVolt - avatar
+ 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.
10th May 2024, 5:05 PM
Lothar
Lothar - avatar
+ 3
SeaWater , isalpha() returns True for all letters, but not for punctuation and not for spaces.
10th May 2024, 5:13 PM
Lothar
Lothar - avatar
+ 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))
11th May 2024, 2:03 AM
JaiprakashJH
JaiprakashJH - avatar
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
10th May 2024, 2:32 PM
SeaWater
SeaWater - avatar
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.)
10th May 2024, 4:42 PM
SeaWater
SeaWater - avatar
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. )
10th May 2024, 5:10 PM
SeaWater
SeaWater - avatar
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.
10th May 2024, 5:20 PM
SeaWater
SeaWater - avatar
0
Congratulations on solving the main test cases! The hidden test cases might be related to edge cases or input validation. Here are some possibilities: 1. *Empty string input*: Your code might not handle an empty string ("") as input. Consider adding a check for this case and returning 0 or a suitable value. 2. *Non-string input*: The challenge might test your code with non-string inputs (e.g., numbers, null, or undefined). Ensure you have input validation to handle such cases. 3. *String with only punctuation*: If the input string contains only punctuation marks (e.g., "!?,."), your code might not handle it correctly. 4. *String with numbers*: If the input string contains numbers (e.g., "hello123"), your code should ignore them when calculating the average word length. 5. *String with multiple spaces between words*: Your code might not handle strings with multiple consecutive spaces correctly. 6. *String with leading or trailing spaces*: Ensure your code trims leading and trailing spaces from the input
12th May 2024, 6:02 AM
Precious Chisom
Precious Chisom - avatar
0
<happy mother's dayp/>
12th May 2024, 6:25 AM
Innocent boy
Innocent boy - avatar