問題
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".
制約
1 ≤ length ≤ 10,000
例
入力:
text = "One fish. Two fish. Red fish. Blue fish."
期待される出力:
expected = "Before Grade 1"
入力:
std::string text = "One fish. Two fish. Red fish. Blue fish.";
期待される出力:
std::string expected = "Before Grade 1";
入力:
string text = "One fish. Two fish. Red fish. Blue fish.";
期待される出力:
string expected = "Before Grade 1";
解説
29 letters, 8 words and 4 sentences give an index below 1: "Before Grade 1".
入力:
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."
期待される出力:
expected = "Grade 5"
入力:
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.";
期待される出力:
std::string expected = "Grade 5";
入力:
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.";
期待される出力:
string expected = "Grade 5";
解説
The average word and sentence lengths give an index that rounds to 5: "Grade 5".
解き方のヒント
まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。
ヒント 1
同じテキストから、文字数・単語数・文数をそれぞれ数えます。
ヒント 2
指定された指数式で、100語あたりの文字数と100語あたりの文数を使います。
ヒント 3
出力ラベルを選ぶ前に、最も近い整数に丸めます。
目安の計算量
時間 O(n)、追加メモリ O(1)
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。