問題
Coleman-Liau 指数は、テキストがアメリカの何年生向けかを推定する指標です。index = 0.0588 × L − 0.296 × S − 15.8 で計算します。ここで L は 100 語あたりの平均文字数、S は 100 語あたりの平均文数です。文字は A-Z/a-z の英字とし、単語はスペースで区切られ、.、!、? のそれぞれが 1 つの文の終わりを表します。指数は最も近い整数に丸めてください。
readability_grade(text) を実装してください。
入力
text: 少なくとも 1 語を含む英語のテキストです。
出力
指数が 1 未満なら "Before Grade 1"、16 以上なら "Grade 16+"、それ以外なら "Grade N" を返してください。
制約
1 ≤ 長さ ≤ 10,000
例
例 1
入力:
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 文字、8 語、4 文から計算すると指数は 1 未満になります。答えは "Before Grade 1" です。
例 2
入力:
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";
解説
単語と文の平均的な長さから計算した指数を丸めると 5 になります。答えは "Grade 5" です。
解き方のヒント
まずは自分で考えてから、ヒントを1つずつ開いてみましょう。
ヒント 1
同じテキストから、文字数・単語数・文数をそれぞれ数えます。
ヒント 2
指定された指数式で、100語あたりの文字数と100語あたりの文数を使います。
ヒント 3
出力ラベルを選ぶ前に、最も近い整数に丸めます。
目安の計算量
時間 O(n)、追加メモリ O(1)
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。