1
+ using System ;
2
+ using System . Collections ;
3
+ using System . Collections . Generic ;
4
+ using System . Linq ;
5
+ using System . Text . RegularExpressions ;
6
+ using System . Xml . Linq ;
7
+ using NUnit . Framework ;
8
+ // Task 6
9
+ // Write a program calculate average words length
10
+ namespace CodingInterview
11
+ {
12
+ public class Class6
13
+ {
14
+
15
+ private static string RemoveSpecialCharacters ( string message )
16
+ {
17
+ Regex regex = new Regex ( "[^a-zA-Z0-9 ]" ) ;
18
+ return regex . Replace ( message , "" ) ;
19
+ }
20
+
21
+ private static double AverageWordsLength ( string message )
22
+ {
23
+ var messageWithoutSpecialCharacters = RemoveSpecialCharacters ( message ) ;
24
+ var words = messageWithoutSpecialCharacters . Split ( ' ' ) ;
25
+ var wordLengthSum = 0 ;
26
+ foreach ( var word in words )
27
+ {
28
+ wordLengthSum = wordLengthSum + word . Length ;
29
+ }
30
+ var response = ( double ) wordLengthSum / words . Length ;
31
+ return Math . Round ( response , 2 ) ;
32
+ }
33
+
34
+
35
+ [ Test ]
36
+ [ TestCase ( "Hi all, my name is Tom...I am originally from Australia." , 4.2 ) ]
37
+ [ TestCase ( "I need to work very hard to learn more about algorithms in Python!" , 4.08 ) ]
38
+ public void CheckAverageWordsLength ( string input , double expected_value )
39
+ {
40
+ Assert . AreEqual ( AverageWordsLength ( input ) , expected_value ) ;
41
+ }
42
+
43
+ }
44
+
45
+ }
0 commit comments