Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7e375c

Browse files
committed
Create test_6_average_words_length.cs
1 parent f8e7cf8 commit e7e375c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /