Statistics

Problem Statement for "TextStatistics"

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.

Definition

Class:
TextStatistics
Method:
averageLength
Parameters:
String
Returns:
double
Method signature:
double averageLength(String text)
(be sure your method is public)

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

  1. "This is div2 easy problem."

    Returns: 4.0

    The example from the problem statement.

  2. "Hello, world!"

    Returns: 5.0

    In this case all words have the same length.

  3. "Simple"

    Returns: 6.0

    One word.

  4. ""

    Returns: 0.0

    No words here, so return 0.

  5. "a bc"

    Returns: 1.5

  6. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    Returns: 26.0

  7. "abcdefghijklmnopqrstuvwxyz"

    Returns: 26.0

  8. "a0a"

    Returns: 1.0

  9. "a,a"

    Returns: 1.0

  10. ","

    Returns: 0.0

  11. "0123456789"

    Returns: 0.0

  12. ",.?!-"

    Returns: 0.0

  13. "0 1 2 3 4 5 6 7 8 9"

    Returns: 0.0

  14. " "

    Returns: 0.0

  15. " "

    Returns: 0.0

  16. " "

    Returns: 0.0

  17. "01234567890123456789012345678901234567890123456789"

    Returns: 0.0

  18. "a0a1a2a3a4a5a6a7a8a9a,a!a?a.a-a a"

    Returns: 1.0

  19. "a a bc"

    Returns: 1.3333333333333333

  20. "a a"

    Returns: 1.0

  21. "a,ab,b,bc,c"

    Returns: 1.4

  22. "w"

    Returns: 1.0

  23. "JD"

    Returns: 2.0

  24. "H1I"

    Returns: 1.0

  25. "rOhk"

    Returns: 4.0

  26. "zeu5w"

    Returns: 2.0

  27. "4NYuxn"

    Returns: 5.0

  28. "ib3YNjr"

    Returns: 3.0

  29. "UJtoqTEv"

    Returns: 8.0

  30. "WsLeMVw51"

    Returns: 7.0

  31. "HLD!jWmU92"

    Returns: 3.5

  32. "wqpZyuGrKa7"

    Returns: 10.0

  33. "Z1a4i3ubut2u"

    Returns: 1.6

  34. "xylPoNXQkF9qt"

    Returns: 6.0

  35. "Eaq?Argjb!G3ah"

    Returns: 2.75

  36. "s27U0W7vikUOaup"

    Returns: 2.75

  37. "lX1APDMgvGofjLm-"

    Returns: 7.0

  38. "T,P03Ywm593k6drqI"

    Returns: 2.0

  39. "9QW.iz qWUz Dl!-FQ"

    Returns: 2.4

  40. "G2LfEIitZtyHIYdmOO4"

    Returns: 8.5

  41. ",GJQ8Bz ,btCN?uw!za9"

    Returns: 2.6

  42. "7Ph!KvQFcNwvxlvcO4qhJ"

    Returns: 6.0

  43. "-svj56SeNLL4Zr Jq8AhQC"

    Returns: 3.2

  44. "hFLd-us9Y-Ag vss7 Cx?hA"

    Returns: 2.2857142857142856

  45. "7-01vcntzZv!OkV7Uqnf35Wt"

    Returns: 4.0

  46. "XVhGBYaLbZGxOimPaUJEmQuzB"

    Returns: 25.0

  47. "oY!pQEnLMrXuEs587eqOgaBZ!1"

    Returns: 6.666666666666667

  48. "z1z.ew7Iy!IrcW1-7FO-6eclUtm"

    Returns: 2.5714285714285716

  49. "FmK44e2Q2 eU!wOi7s-R99Thq?sS"

    Returns: 1.8888888888888888

  50. "aOzAzyFJWvTaoX1-VZ7ZtJMdo4DFf"

    Returns: 6.25

  51. "0j9Wf4Fm6i7JOobPUxT,,uy GB?GL5"

    Returns: 2.5

  52. "w84lFC1hD2ot2?43 Jnw67Kmt8KhOQn"

    Returns: 2.7142857142857144

  53. "4p?js3jmkP2B4fSivba2tqpjsNgWlZy6"

    Returns: 4.166666666666667

  54. "9iOMOcG4,8!nLKgbyZof?y5ITZ CHZH?p"

    Returns: 4.0

  55. "t4-EecQ6a1K3,yyHM-?YstvBVvEB8t,R6m"

    Returns: 2.5555555555555554

  56. " ivhHxE6!6nD.upTjLEgpnF?pHaoA3ZwOST"

    Returns: 5.6

  57. "qYIQ4sYzQkD?9ZH? 7.3OPKG8m!ANhmlen6W"

    Returns: 3.5714285714285716

  58. "cRT1 OhA0lWkSKOq ,7AZHGGfnCH.B.udLp11"

    Returns: 4.5

  59. "2AHaAgWwy1t7C.6vSVfoJ06ajkx0NPs.STH5op"

    Returns: 3.5

  60. "GAmMGmV-NY4cgPvX?eoG7al DnvxZ4BS7OOOG1,"

    Returns: 3.75

  61. "mUrx7-kuz1DaOlW2wbvXczn,e!u815aYROwB,ZWb"

    Returns: 3.75

  62. "fCgvwOw?K6HpMs.2n,IfHlRmkyj9dk FauX?oVzXB"

    Returns: 4.125

  63. "bc j-TElnldjec64i6yD,d38,nNGXvyAyHMqKpBvCW"

    Returns: 4.714285714285714

  64. ".q09?l4YADbNm2OgLr3,UwYe-dIh54sJzL079D4gWfg"

    Returns: 3.111111111111111

  65. "aRaWpO5N6lwilVlVEpND UJxlPpbfhAFuqZS9VlDAelN"

    Returns: 8.0

  66. "ZUTwCTiIOsx9P1hMfbGuVX.VqI0,!TuNtKt3ww9CfhVpU"

    Returns: 5.285714285714286

  67. "69mZF6TK!x W70Wa1mDDRdo!s OzLhLNGAz zJbo,2H.i-"

    Returns: 2.8181818181818183

  68. "9DDnG?H nRz6XbuD,fk9v7xdDqlxzunCMymDaoYg2ojAYDr"

    Returns: 4.875

  69. "-MxruFgh-04GhRJS12aPUzH3y3oabiuUfXsOYaBsxqXrkOMc"

    Returns: 8.0

  70. "SZ!Vl4ZaUtfsgSoo,0Ch96o?WL9OzhCizljgA-ILb??kGbdC!"

    Returns: 4.111111111111111

  71. "6vFPhGTGg3viQRsw?N04X!YAyaGn9L4ZCo-PFRwFM.7o5wozSN"

    Returns: 3.8

  72. "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"

    Returns: 50.0

  73. "asdf2asdf"

    Returns: 4.0

  74. "asdf?asdf"

    Returns: 4.0

  75. "This is div2 easy problem."

    Returns: 4.0

  76. "09326732,00,1, -?."

    Returns: 0.0

  77. "su2kiran"

    Returns: 3.5

  78. "This is simple"

    Returns: 4.0

  79. "This is div2------ easy problem."

    Returns: 4.0

  80. "asge4gdhr"

    Returns: 4.0

  81. "a2xx"

    Returns: 1.5

  82. "!!!"

    Returns: 0.0

  83. "a,b,c,d,e,"

    Returns: 1.0

  84. "s........"

    Returns: 1.0

  85. "t l ha "

    Returns: 1.3333333333333333

  86. "a-a"

    Returns: 1.0

  87. "as23 asd21 asd3?? as "

    Returns: 2.5

  88. ".."

    Returns: 0.0

  89. "abc.ab"

    Returns: 2.5

  90. ""

    Returns: 0.0

  91. "w.w"

    Returns: 1.0

  92. "a?bc"

    Returns: 1.5

  93. "a - - -ab a, "

    Returns: 1.3333333333333333

  94. "i am a boy"

    Returns: 1.75

  95. "mac9mac9mac9"

    Returns: 3.0

  96. "aaa bb"

    Returns: 2.5

  97. "thisisatestcasey?tescase?"

    Returns: 11.5

  98. "a-a-a"

    Returns: 1.0

  99. "a9a-"

    Returns: 1.0

  100. "A...AA.AA.....a"

    Returns: 1.5

  101. "aaa ... bbb"

    Returns: 3.0

  102. " "

    Returns: 0.0

  103. " "

    Returns: 0.0

  104. "ahmed.a"

    Returns: 3.0

  105. "."

    Returns: 0.0

  106. "a 1 2 3 4 5 6"

    Returns: 1.0

  107. "a2a"

    Returns: 1.0

  108. "the most-important thing"

    Returns: 5.25

  109. "!?41 12384 ?!,. ?"

    Returns: 0.0

  110. "Hello 72 worlds!"

    Returns: 5.5

  111. "Hello, worlds!"

    Returns: 5.5

  112. "hi4 text test. challenge! . ! ?"

    Returns: 4.75

  113. "diego salas "

    Returns: 5.0

  114. "diego salas"

    Returns: 5.0

  115. "This 3i4a3 pA7w0rd077hi7.91a9.?aa0a"

    Returns: 1.7

  116. "Mo!ahmed!allah"

    Returns: 4.0

  117. "Mohamed G"

    Returns: 4.0

  118. "Mohamed1F"

    Returns: 4.0

  119. "a!l!i!!"

    Returns: 1.0

  120. "a a"

    Returns: 1.0

  121. ","

    Returns: 0.0

  122. "zzz! !ZZ! AA!- - ! z ."

    Returns: 2.0

  123. "Hello, worldadaewraea a0fasddf1ad1ad2ad"

    Returns: 4.571428571428571

  124. " word word2"

    Returns: 4.0

  125. "a"

    Returns: 1.0

  126. "Challenge1989, phase by Al"

    Returns: 4.5

  127. ",,,,!!!"

    Returns: 0.0

  128. "absa wef newaklfn fewan dafk vkldsaj w ek k sdl fw"

    Returns: 3.6363636363636362

  129. "my name is a nkur"

    Returns: 2.6

  130. "a!a"

    Returns: 1.0

  131. "!"

    Returns: 0.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: