TopCoder problem "Hyphenated" used in SRM 202 (Division I Level One , Division II Level Two)



Problem Statement

    We want to be able to judge whether text is suitable for a particular age group. We will base our judgment on the average length of a word in the text, so we need to define what a "word" is.

We define a "word" to be a maximal sequence of letters ('A'-'Z' and/or 'a-z') within a single line. (Maximal means that if 2 letters are adjacent within a line, they are in the same word.) But if a line ends with a sequence of one or more letters immediately followed by a hyphen ('-') and the next line starts with a sequence of one or more letters, all these letters are considered to be in the same word. It is even possible that a hyphenated word could extend across several lines (see Example 2).

Create a class Hyphenated that contains a method avgLength that is given a String[] lines containing the lines of text and that returns the average length of the words within the text.

 

Definition

    
Class:Hyphenated
Method:avgLength
Parameters:String[]
Returns:double
Method signature:double avgLength(String[] lines)
(be sure your method is public)
    
 

Notes

-A return value with either an absolute or relative error of less than 1.0E-9 is considered correct.
 

Constraints

-lines will contain between 1 and 50 elements inclusive.
-Each element of lines will contain between 1 and 50 characters inclusive.
-Each character in each element of lines will be a letter ('A-'Z' or 'a'-'z') or will be one of these: ' ', '-', '.'
-At least one element of lines will contain a letter.
 

Examples

0)
    
{"  now is the ex-", "ample.  "} 
Returns: 3.75
There are 4 words: now, is, the, example
1)
    
{"  now is the ex-", " ample.  "}
Returns: 3.0
There are 5 words: now, is, the, ex, ample Note that the leading blank prevents the joining of ex and ample. Also note that words only consist of letters, so the hyphen is never a part of a word.
2)
    
{"inter-","national-","ization.."}
Returns: 20.0
There is only one word.
3)
    
{"All the time I have well-defined  "," trouble."}
Returns: 4.125
Note that well-defined consists of 2 separate words.
4)
    
{"hello-","-","-","-","great"}
Returns: 5.0

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=1911

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5848&pm=1911

Writer:

dgoodman

Testers:

lbackstrom , brett1479

Problem categories:

String Parsing