Statistics

Problem Statement for "Hyphenated"

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

  1. {" now is the ex-", "ample. "}

    Returns: 3.75

    There are 4 words: now, is, the, example

  2. {" 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.

  3. {"inter-","national-","ization.."}

    Returns: 20.0

    There is only one word.

  4. {"All the time I have well-defined "," trouble."}

    Returns: 4.125

    Note that well-defined consists of 2 separate words.

  5. {"...-","all...some-","thing- "," ","b.i. .g"}

    Returns: 3.0

  6. {"a---"," ---","a bc"}

    Returns: 1.3333333333333333

  7. {"---","---","a bc"}

    Returns: 1.5

  8. {"a-","b--","c.-"}

    Returns: 1.5

  9. {"-adssfssfsfsfsfssfsfs-","b--","c-","-"}

    Returns: 11.0

  10. {"-a-d-s-sfssfsfsfsfssfsfs-","-b--","c-","-"}

    Returns: 3.6666666666666665

  11. {" "," ","a"}

    Returns: 1.0

  12. {"here are two-fold meaningsS."}

    Returns: 4.6

  13. {"a-","b-","c-","d-","e-","f-"," g-","h."}

    Returns: 4.0

  14. {"A fine sentence.","A fine sentence","-ending."}

    Returns: 4.571428571428571

  15. {"-A -fine sentence.","A fine sentence-","-ending."}

    Returns: 4.571428571428571

  16. {"hello-","-","-","-","great"}

    Returns: 5.0

  17. {"un-done-","-","abc-de--","good","-","bad.person"}

    Returns: 3.4285714285714284

  18. {"A-B","C"}

    Returns: 1.0

  19. {"A","B"}

    Returns: 1.0

  20. {"abc","def","abcdefg"}

    Returns: 4.333333333333333

  21. { " now is the ex-", "ample. " }

    Returns: 3.75

  22. { "hello-", "-", "-", "-", "great" }

    Returns: 5.0

  23. { "-a-", "-- - - --", "-- -b-" }

    Returns: 1.0

  24. { "AAA-", "-", "AAA" }

    Returns: 3.0

  25. { "A-B", "C" }

    Returns: 1.0

  26. { "a", "-", "ba" }

    Returns: 1.5

  27. { "hello.world" }

    Returns: 5.0

  28. { "a - h", "b -ff - - ff -", "- - -" }

    Returns: 1.4

  29. { ". x" }

    Returns: 1.0

  30. { "apple", "apple" }

    Returns: 5.0

  31. { "All the time I have well-defined ", " trouble.", "-aaaaaaa-", "bbb -bb ---aaa" }

    Returns: 4.363636363636363

  32. { "help--", "me" }

    Returns: 3.0

  33. { "a - - -s -", "- ss - -s - sss -", "aaaaa -- - aa" }

    Returns: 2.142857142857143

  34. { "All the time I have well-defined ", " trouble.", "a-a-a-a-a-a aaaaaaaaa", "bbbbbbbb bbbbbbbbbbbb-", " cccccccccccccc" }

    Returns: 4.555555555555555

  35. { "hello-", "-", "-", "-", "great", "mazin", "ma-asdf-", "gfadsfga" }

    Returns: 5.8

  36. { "a b-c", "-", "b-", "c", "b-", "-c", "b-", "-c-", "d", " d-", "e ", "b", "c", "d" }

    Returns: 1.25

  37. { "asdf-bad", "asdf-bad-", "asdf bad", "asdf bad-" }

    Returns: 4.0

  38. { "abc.def" }

    Returns: 3.0

  39. { "he-", "-", "-l", "n.i-", " s.da-", "-", "-sdml .-", " bla-" }

    Returns: 1.875

  40. { "All the time I have well-defined ", " trouble.", "aaaaa aaaaaaaaaaaaaaaa-", "aacc c cccccccccccccccc", "-aaaaaaaaaaaaaaa ----", "a-a-a-a--a-a-a--a-a-a--a-a-a", "aaaaaaaaaaaa-", " bbbbbbbbbbb" }

    Returns: 4.5

  41. { "a", "b" }

    Returns: 1.0

  42. { "ab", "cd", "ef" }

    Returns: 2.0

  43. { "a.a" }

    Returns: 1.0

  44. { "a aa sss -- -", "a dasd aasa -", "aa aa -", "aaa ss" }

    Returns: 2.4

  45. { "a", "-", "b" }

    Returns: 1.0

  46. { "abi", "-", "abi" }

    Returns: 3.0

  47. { "All the time I have well-defined ", " trouble.", "aaaaaaaaaaaaaaa-aaaaaaaaaa", "aaaaaaaaaaaaa aaaaaaaaaa-", " cccccccccccccc", "asaa--a-a-a-a--a-a--a-a-a--a-a-a-a--a-a-a", "aaaaaa aaaaaaaaaaaa-", "a" }

    Returns: 4.1875

  48. { "abc-" }

    Returns: 3.0

  49. { "booo -", "weee" }

    Returns: 4.0

  50. { "a-" }

    Returns: 1.0

  51. { "oxi", "foximotomox-" }

    Returns: 7.0

  52. { "aaa", "aaa", "aaa" }

    Returns: 3.0

  53. { "All the time I have well-defined ", " trouble.", "a-a-a-a-a-a-", "--", "-", "-", "aaaaaaa-", " bbb bbb " }

    Returns: 3.0588235294117645

  54. {"abc", "def", "." }

    Returns: 3.0

  55. {"A", "-", "B" }

    Returns: 1.0

  56. {" now is the ex--", "ample. " }

    Returns: 3.0

  57. {"ab", "cd" }

    Returns: 2.0

  58. {"abc", "def" }

    Returns: 3.0

  59. {"a", "-", "a" }

    Returns: 1.0

  60. {"a", "b" }

    Returns: 1.0

  61. {"kyuri", "de", "namida", ".", "perorin" }

    Returns: 5.0

  62. {"a.b" }

    Returns: 1.0

  63. {"a", "a" }

    Returns: 1.0

  64. {"aa", "aa" }

    Returns: 2.0

  65. {"-a-" }

    Returns: 1.0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: