Problème
L'indice de Coleman-Liau estime le niveau scolaire américain (grade) correspondant à un texte : indice = 0.0588 × L − 0.296 × S − 15.8, où L est le nombre moyen de lettres pour 100 mots et S le nombre moyen de phrases pour 100 mots. Les lettres sont A-Z/a-z, les mots sont séparés par des espaces, et chaque ., ! ou ? termine une phrase. Arrondissez l'indice à l'entier le plus proche.
Implémentez readability_grade(text).
Entrée
text : un texte en anglais contenant au moins un mot.
Sortie
Renvoyez "Before Grade 1" si l'indice est inférieur à 1, "Grade 16+" s'il vaut 16 ou plus, et "Grade N" sinon.
Contraintes
1 ≤ longueur ≤ 10,000
Exemples
Entrée :
text = "One fish. Two fish. Red fish. Blue fish."
Sortie attendue :
expected = "Before Grade 1"
Entrée :
std::string text = "One fish. Two fish. Red fish. Blue fish.";
Sortie attendue :
std::string expected = "Before Grade 1";
Entrée :
string text = "One fish. Two fish. Red fish. Blue fish.";
Sortie attendue :
string expected = "Before Grade 1";
Explication
29 lettres, 8 mots et 4 phrases donnent un indice inférieur à 1 : "Before Grade 1".
Entrée :
text = "Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard."
Sortie attendue :
expected = "Grade 5"
Entrée :
std::string text = "Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.";
Sortie attendue :
std::string expected = "Grade 5";
Entrée :
string text = "Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.";
Sortie attendue :
string expected = "Grade 5";
Explication
Les longueurs moyennes des mots et des phrases donnent un indice qui s'arrondit à 5 : "Grade 5".
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Compte séparément les lettres, les mots et les phrases à partir du même texte.
Indice 2
Utilise la formule d’indice donnée avec les lettres pour 100 mots et les phrases pour 100 mots.
Indice 3
Arrondis à l’entier le plus proche avant de choisir le libellé de sortie.
Complexité attendue
temps O(n), espace supplémentaire O(1)
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.