Médio 0 resolvidos 0% de aceitação

Readability Grade

Estimate the school grade needed to read a text.

O que você vai aprender

  • Contar letras, palavras e frases
  • Aplicar a fórmula de Coleman-Liau
  • Arredondar e limitar um nível escolar

Problema

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".

Restrições

1 ≤ length ≤ 10,000

Exemplos

Exemplo 1
Entrada:
text = "One fish. Two fish. Red fish. Blue fish."

Saída esperada:
expected = "Before Grade 1"
Entrada:
std::string text = "One fish. Two fish. Red fish. Blue fish.";

Saída esperada:
std::string expected = "Before Grade 1";
Entrada:
string text = "One fish. Two fish. Red fish. Blue fish.";

Saída esperada:
string expected = "Before Grade 1";

Explicação

29 letters, 8 words and 4 sentences give an index below 1: "Before Grade 1".

Exemplo 2
Entrada:
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."

Saída esperada:
expected = "Grade 5"
Entrada:
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.";

Saída esperada:
std::string expected = "Grade 5";
Entrada:
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.";

Saída esperada:
string expected = "Grade 5";

Explicação

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

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

Conte letras, palavras e sentenças separadamente no mesmo texto.

Dica 2

Use a fórmula do índice fornecida com letras por 100 palavras e sentenças por 100 palavras.

Dica 3

Arredonde para o inteiro mais próximo antes de escolher a classificação de saída.

Complexidade esperada

tempo O(n), espaço extra O(1)

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.