Problema
L'indice di Coleman-Liau stima il livello scolastico statunitense (grade) adatto a un testo: indice = 0.0588 × L − 0.296 × S − 15.8, dove L è il numero medio di lettere ogni 100 parole e S il numero medio di frasi ogni 100 parole. Le lettere sono A-Z/a-z, le parole sono separate da spazi e ogni ., ! o ? chiude una frase. Arrotonda l'indice all'intero più vicino.
Implementa readability_grade(text).
Input
text: un testo in inglese con almeno una parola.
Output
Restituisci "Before Grade 1" quando l'indice è minore di 1, "Grade 16+" quando è 16 o più, altrimenti "Grade N".
Vincoli
1 ≤ lunghezza ≤ 10,000
Esempi
Input:
text = "One fish. Two fish. Red fish. Blue fish."
Output atteso:
expected = "Before Grade 1"
Input:
std::string text = "One fish. Two fish. Red fish. Blue fish.";
Output atteso:
std::string expected = "Before Grade 1";
Input:
string text = "One fish. Two fish. Red fish. Blue fish.";
Output atteso:
string expected = "Before Grade 1";
Spiegazione
29 lettere, 8 parole e 4 frasi danno un indice minore di 1: "Before Grade 1".
Input:
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."
Output atteso:
expected = "Grade 5"
Input:
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.";
Output atteso:
std::string expected = "Grade 5";
Input:
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.";
Output atteso:
string expected = "Grade 5";
Spiegazione
Le lunghezze medie di parole e frasi danno un indice che arrotondato vale 5: "Grade 5".
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Conta separatamente lettere, parole e frasi nello stesso testo.
Suggerimento 2
Usa la formula dell'indice data con lettere per 100 parole e frasi per 100 parole.
Suggerimento 3
Arrotonda al numero intero più vicino prima di scegliere l'etichetta di output.
Complessità attesa
tempo O(n), spazio aggiuntivo O(1)
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.