Problem: Why does my code not work for Code coach "Average Word length"
I wrote that Code and used the first time List<>. I wrote the Base in VS Community and rewrote it in App but now some 'hidden' problems didn't get solved. Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sololearn { class Program { public static List<int> count = new List<int>(); static void Main(string[] args) { string[] str = Console.ReadLine().Split(" "); foreach(string wrd in str) { count.Add(wrd.Count(x => wrd.Contains(x))); } Console.WriteLine("{0:0}", count.Average()); } } }
5/26/2022 10:25:53 AM
Shi
9 Answers
New AnswerI used this to count only letters: if (Char.IsLetter(c)) If i comment it out the first test fails.
Shi Did you remove the spaces from the letter count. The spaces between letters is considered to be a character, which will be counted, and will skew your results
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sololearn { class Program { public static List<int> count = new List<int>(); static void Main(string[] args) { string[] str = Console.ReadLine().Split(" "); foreach(string wrd in str) { count.Add(wrd.Count(x => wrd.Contains(x).Equals(char.IsLetter(x)))); } Console.WriteLine(Math.Ceiling(count.Average())); } } }
I did already round Up and it still didn't Work. But thanks all of you, Paul and Jascript, I finished now and that because of you. My Problem got solved with isLetter and Math.Ceiling! ^^
I already did round that's the meaning of "{0:0}" it force the whole number out. And I also did Math.Round() but didn't Work.
I did try now with isLetter but now even less solutions got solved.. my code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sololearn { class Program { public static List<int> count = new List<int>(); static void Main(string[] args) { int res = 0; string[] str = Console.ReadLine().Split(" "); foreach(string wrd in str) { char[] arr = wrd.ToCharArray(); for(int i = 0; i < wrd.Length; i++) { if(char.IsLetter(arr[i])) res++; } } count.Add(res); res = 0; } Console.WriteLine(Math.Round(count.Average())); } } }