Connection Status:
Competition Arena > TextStatistics
SRM 332 · 2006-12-28 · by andrewzta · Brute Force, Simple Math
Class Name: TextStatistics
Return Type: double
Method Name: averageLength
Arg Types: (string)
Problem Statement

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 String text, return the average word length in it. If there are no words in the text, return 0.0.

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: ',', '.', '?', '!', '-'.
Examples
0)
"This is div2 easy problem."
Returns: 4.0

The example from the problem statement.

1)
"Hello, world!"
Returns: 5.0

In this case all words have the same length.

2)
"Simple"
Returns: 6.0

One word.

3)
""
Returns: 0.0

No words here, so return 0.

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

Coding Area

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

Submitting as anonymous