+ 1

Average Word Length problem C# test 3 and 5 fail

I think it have connection with rounding. When i use Math.Round i get 2 fails (test 3 and 5) but when use Math.Ceiling i have only one fail (in test 1) My code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string sentence = Console.ReadLine(); List<char> word = new List<char>(); List<int> sum = new List<int>(); int i = 0; double result; while(i <= sentence.Length-1) { if (sentence[i].Equals(' ') || sentence.Equals(',') || sentence.Equals('.')) { sum.Add(word.Count); word.Clear(); i++; } else { word.Add(sentence[i]); i++; } } sum.Add(word.Count); result = Math.Round(sum.Average()); Console.WriteLine(result); //Console.ReadLine(); } } }

20th Nov 2020, 8:06 PM
Mr Wolfy
Mr Wolfy - avatar
4 Réponses
+ 2
In this code coach the output should be the nearest integer greater than or equal to the average. Just change Math.round() to Math.ceil()
20th Nov 2020, 9:17 PM
Davide
Davide - avatar
+ 2
Davide if i use Math.Ceiling (only this metode is in C#) i have fail in first test, correct answer is 3 and my code gives 4. The average in first case is 3,80 so Math.Ceiling it rounds well to 4 but SoloLearn considers this fail :/
21st Nov 2020, 2:45 PM
Mr Wolfy
Mr Wolfy - avatar
+ 2
Unfortunately I can't debug your program because I don't know C#, but I think that the problem is that you are counting spaces as parts of words.
21st Nov 2020, 3:19 PM
Davide
Davide - avatar
+ 1
I checked the issue of space on start because i have list with length of each word and thise numbers are correct (no space, ",", "." ) :/
21st Nov 2020, 8:37 PM
Mr Wolfy
Mr Wolfy - avatar