Tous les défis
Moyen 1 résolu 100.0 % de réussite

Readability Grade

Estimate the school grade needed to read a text.

Ce que vous allez apprendre

  • Count letters, words and sentences
  • Apply the Coleman-Liau formula
  • Round and clamp a grade

Problème

Afficher la traduction

The Coleman-Liau index estimates the U.S. grade level for a text: index = 0.0588 × L − 0.296 × S − 15.8, where L is the average number of letters per 100 words and S the average number of sentences per 100 words. Letters are A–Z/a–z, words are separated by spaces, and each ., ! or ? ends a sentence. Round the index to the nearest whole number.

Implement readability_grade(text).

Input

text: English text with at least one word.

Output

Return "Before Grade 1" when the index is below 1, "Grade 16+" when it is 16 or more, otherwise "Grade N".

Contraintes

1 ≤ length ≤ 10,000

Exemples

Exemple 1
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 letters, 8 words and 4 sentences give an index below 1: "Before Grade 1".

Exemple 2
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

The average word and sentence lengths give an index that rounds to 5: "Grade 5".

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Count letters, words, and sentences separately from the same text.

Indice 2

Use the given index formula with letters per 100 words and sentences per 100 words.

Indice 3

Round to the nearest whole number before choosing the output label.

Complexité attendue

O(n) time, O(1) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

# Codeur Langage Temps Mémoire Note de l'IA Solution
1 Н"Никита "Programmer" Python 1,033 ms - 5/5 Voir le code

Apprenez des autres

Solutions de la communauté

Attention spoiler : 1 solution complète ci-dessous Essayez d'abord de le résoudre vous-même. Vous pouvez toujours jeter un œil au code des autres développeurs et aux avis IA. Connectez-vous et résolvez le défi pour rejoindre la discussion.