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
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
"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
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Returns: 26.0
"abcdefghijklmnopqrstuvwxyz"
Returns: 26.0
"a0a"
Returns: 1.0
"a,a"
Returns: 1.0
","
Returns: 0.0
"0123456789"
Returns: 0.0
",.?!-"
Returns: 0.0
"0 1 2 3 4 5 6 7 8 9"
Returns: 0.0
" "
Returns: 0.0
" "
Returns: 0.0
" "
Returns: 0.0
"01234567890123456789012345678901234567890123456789"
Returns: 0.0
"a0a1a2a3a4a5a6a7a8a9a,a!a?a.a-a a"
Returns: 1.0
"a a bc"
Returns: 1.3333333333333333
"a a"
Returns: 1.0
"a,ab,b,bc,c"
Returns: 1.4
"w"
Returns: 1.0
"JD"
Returns: 2.0
"H1I"
Returns: 1.0
"rOhk"
Returns: 4.0
"zeu5w"
Returns: 2.0
"4NYuxn"
Returns: 5.0
"ib3YNjr"
Returns: 3.0
"UJtoqTEv"
Returns: 8.0
"WsLeMVw51"
Returns: 7.0
"HLD!jWmU92"
Returns: 3.5
"wqpZyuGrKa7"
Returns: 10.0
"Z1a4i3ubut2u"
Returns: 1.6
"xylPoNXQkF9qt"
Returns: 6.0
"Eaq?Argjb!G3ah"
Returns: 2.75
"s27U0W7vikUOaup"
Returns: 2.75
"lX1APDMgvGofjLm-"
Returns: 7.0
"T,P03Ywm593k6drqI"
Returns: 2.0
"9QW.iz qWUz Dl!-FQ"
Returns: 2.4
"G2LfEIitZtyHIYdmOO4"
Returns: 8.5
",GJQ8Bz ,btCN?uw!za9"
Returns: 2.6
"7Ph!KvQFcNwvxlvcO4qhJ"
Returns: 6.0
"-svj56SeNLL4Zr Jq8AhQC"
Returns: 3.2
"hFLd-us9Y-Ag vss7 Cx?hA"
Returns: 2.2857142857142856
"7-01vcntzZv!OkV7Uqnf35Wt"
Returns: 4.0
"XVhGBYaLbZGxOimPaUJEmQuzB"
Returns: 25.0
"oY!pQEnLMrXuEs587eqOgaBZ!1"
Returns: 6.666666666666667
"z1z.ew7Iy!IrcW1-7FO-6eclUtm"
Returns: 2.5714285714285716
"FmK44e2Q2 eU!wOi7s-R99Thq?sS"
Returns: 1.8888888888888888
"aOzAzyFJWvTaoX1-VZ7ZtJMdo4DFf"
Returns: 6.25
"0j9Wf4Fm6i7JOobPUxT,,uy GB?GL5"
Returns: 2.5
"w84lFC1hD2ot2?43 Jnw67Kmt8KhOQn"
Returns: 2.7142857142857144
"4p?js3jmkP2B4fSivba2tqpjsNgWlZy6"
Returns: 4.166666666666667
"9iOMOcG4,8!nLKgbyZof?y5ITZ CHZH?p"
Returns: 4.0
"t4-EecQ6a1K3,yyHM-?YstvBVvEB8t,R6m"
Returns: 2.5555555555555554
" ivhHxE6!6nD.upTjLEgpnF?pHaoA3ZwOST"
Returns: 5.6
"qYIQ4sYzQkD?9ZH? 7.3OPKG8m!ANhmlen6W"
Returns: 3.5714285714285716
"cRT1 OhA0lWkSKOq ,7AZHGGfnCH.B.udLp11"
Returns: 4.5
"2AHaAgWwy1t7C.6vSVfoJ06ajkx0NPs.STH5op"
Returns: 3.5
"GAmMGmV-NY4cgPvX?eoG7al DnvxZ4BS7OOOG1,"
Returns: 3.75
"mUrx7-kuz1DaOlW2wbvXczn,e!u815aYROwB,ZWb"
Returns: 3.75
"fCgvwOw?K6HpMs.2n,IfHlRmkyj9dk FauX?oVzXB"
Returns: 4.125
"bc j-TElnldjec64i6yD,d38,nNGXvyAyHMqKpBvCW"
Returns: 4.714285714285714
".q09?l4YADbNm2OgLr3,UwYe-dIh54sJzL079D4gWfg"
Returns: 3.111111111111111
"aRaWpO5N6lwilVlVEpND UJxlPpbfhAFuqZS9VlDAelN"
Returns: 8.0
"ZUTwCTiIOsx9P1hMfbGuVX.VqI0,!TuNtKt3ww9CfhVpU"
Returns: 5.285714285714286
"69mZF6TK!x W70Wa1mDDRdo!s OzLhLNGAz zJbo,2H.i-"
Returns: 2.8181818181818183
"9DDnG?H nRz6XbuD,fk9v7xdDqlxzunCMymDaoYg2ojAYDr"
Returns: 4.875
"-MxruFgh-04GhRJS12aPUzH3y3oabiuUfXsOYaBsxqXrkOMc"
Returns: 8.0
"SZ!Vl4ZaUtfsgSoo,0Ch96o?WL9OzhCizljgA-ILb??kGbdC!"
Returns: 4.111111111111111
"6vFPhGTGg3viQRsw?N04X!YAyaGn9L4ZCo-PFRwFM.7o5wozSN"
Returns: 3.8
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
Returns: 50.0
"asdf2asdf"
Returns: 4.0
"asdf?asdf"
Returns: 4.0
"This is div2 easy problem."
Returns: 4.0
"09326732,00,1, -?."
Returns: 0.0
"su2kiran"
Returns: 3.5
"This is simple"
Returns: 4.0
"This is div2------ easy problem."
Returns: 4.0
"asge4gdhr"
Returns: 4.0
"a2xx"
Returns: 1.5
"!!!"
Returns: 0.0
"a,b,c,d,e,"
Returns: 1.0
"s........"
Returns: 1.0
"t l ha "
Returns: 1.3333333333333333
"a-a"
Returns: 1.0
"as23 asd21 asd3?? as "
Returns: 2.5
".."
Returns: 0.0
"abc.ab"
Returns: 2.5
""
Returns: 0.0
"w.w"
Returns: 1.0
"a?bc"
Returns: 1.5
"a - - -ab a, "
Returns: 1.3333333333333333
"i am a boy"
Returns: 1.75
"mac9mac9mac9"
Returns: 3.0
"aaa bb"
Returns: 2.5
"thisisatestcasey?tescase?"
Returns: 11.5
"a-a-a"
Returns: 1.0
"a9a-"
Returns: 1.0
"A...AA.AA.....a"
Returns: 1.5
"aaa ... bbb"
Returns: 3.0
" "
Returns: 0.0
" "
Returns: 0.0
"ahmed.a"
Returns: 3.0
"."
Returns: 0.0
"a 1 2 3 4 5 6"
Returns: 1.0
"a2a"
Returns: 1.0
"the most-important thing"
Returns: 5.25
"!?41 12384 ?!,. ?"
Returns: 0.0
"Hello 72 worlds!"
Returns: 5.5
"Hello, worlds!"
Returns: 5.5
"hi4 text test. challenge! . ! ?"
Returns: 4.75
"diego salas "
Returns: 5.0
"diego salas"
Returns: 5.0
"This 3i4a3 pA7w0rd077hi7.91a9.?aa0a"
Returns: 1.7
"Mo!ahmed!allah"
Returns: 4.0
"Mohamed G"
Returns: 4.0
"Mohamed1F"
Returns: 4.0
"a!l!i!!"
Returns: 1.0
"a a"
Returns: 1.0
","
Returns: 0.0
"zzz! !ZZ! AA!- - ! z ."
Returns: 2.0
"Hello, worldadaewraea a0fasddf1ad1ad2ad"
Returns: 4.571428571428571
" word word2"
Returns: 4.0
"a"
Returns: 1.0
"Challenge1989, phase by Al"
Returns: 4.5
",,,,!!!"
Returns: 0.0
"absa wef newaklfn fewan dafk vkldsaj w ek k sdl fw"
Returns: 3.6363636363636362
"my name is a nkur"
Returns: 2.6
"a!a"
Returns: 1.0
"!"
Returns: 0.0