문제
Coleman-Liau 지수는 글을 읽는 데 필요한 미국 학년 수준을 추정합니다. 지수 = 0.0588 × L − 0.296 × S − 15.8이며, L은 100단어당 평균 글자 수, S는 100단어당 평균 문장 수입니다. 글자는 A-Z/a-z이고, 단어는 공백으로 구분되며, ., !, ?가 나올 때마다 문장이 하나 끝납니다. 지수는 가장 가까운 정수로 반올림합니다.
readability_grade(text)를 구현하세요.
입력
text: 단어가 하나 이상 있는 영어 텍스트.
출력
지수가 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
같은 텍스트에서 글자 수, 단어 수, 문장 수를 각각 세세요.
힌트 2
주어진 지수를 사용해 100단어당 글자 수와 문장 수로 계산하세요.
힌트 3
출력 라벨을 고르기 전에 반올림하세요.
예상 복잡도
시간 O(n), 추가 공간 O(1)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.