TextStatistics
SRM 332 · 2006-12-28 · by andrewzta
Problem Statement
Most modern text editors are able to give some statistics about the text they are editing. One nice statistic is the average word length in the text.
A word is a maximal continuous sequence of letters ('a'-'z', 'A'-'Z'). Words can be separated by spaces, digits, and punctuation marks.
The average word length is the sum of all the words' lengths divided by the total number of words. For example, in the text "This is div2 easy problem.", there are 5 words: "This", "is", "div", "easy", and "problem". The sum of the word lengths is 4+2+3+4+7=20, so the average word length is 20/5=4.
Given a
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- text will contain between 0 and 50 characters, inclusive.
- text will contain only letters ('a'-'z', 'A'-'Z'), digits ('0'-'9'), spaces, and the following punctuation marks: ',', '.', '?', '!', '-'.
"This is div2 easy problem." Returns: 4.0
The example from the problem statement.
"Hello, world!" Returns: 5.0
In this case all words have the same length.
"Simple" Returns: 6.0
One word.
"" Returns: 0.0
No words here, so return 0.
"a bc" Returns: 1.5
Submissions are judged against all 131 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TextStatistics with a public method double averageLength(string text) · 131 test cases · 2 s / 256 MB per case