fork download
  1. using System;
  2. using System.Linq;
  3.  
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var str = "HELLOWHELOLWHАРОЗАУПАЛАНАЛАПУАЗОРА";
  10. var wordLen = 3;
  11. var xxx=Enumerable.Range(0, (int)Math.Ceiling((decimal)str.Length / wordLen)) // Получим число слов
  12. .Select(i => str.Substring(i * wordLen, Math.Min(str.Length-i*wordLen,wordLen)).ToCharArray())// Разобьём строку на части нужной длины (последнее слово - остатком)
  13. .SelectMany(x => x.Select((c,i) => new { index = i, chr = c }))// Разобьём части на символы с запоминанием позиции
  14. .GroupBy(x => x.index) // Сгруппируем в "слова"
  15. .Select((x, i) => new { Word = i, letters = x.GroupBy(g => g.chr) }) //Сгруппируем каждое слово посимвольно
  16. .Select(x => x.letters.Select(l => new { x.Word, l.Key, Count = l.Count() })) //Посчитаем символы
  17. .SelectMany(x => Enumerable.Range('A', 'Z'-'A').Union(Enumerable.Range('А', 'Я'-'А')).Select(e => new //Получим алфавит
  18. {
  19. x.First().Word,
  20. Letter = (char)e,
  21. Rate = (decimal)(x.FirstOrDefault(w => w.Key == e)?.Count??0) / x.Sum(w => w.Count) //Посчитаем рейт
  22. })).Where(x=>x.Rate>0).ToList(); //Выведем все записи с ненулевым рейтом
  23.  
  24. foreach(var x in xxx)
  25. {
  26. Console.WriteLine($"Word: {x.Word}, Letter: {x.Letter}, Rate: {x.Rate}");
  27. }
  28. }
  29. }
Success #stdin #stdout 0.04s 19388KB
stdin
Standard input is empty
stdout
Word: 0, Letter: H, Rate: 0.25
Word: 0, Letter: L, Rate: 0.0833333333333333333333333333
Word: 0, Letter: O, Rate: 0.0833333333333333333333333333
Word: 0, Letter: А, Rate: 0.1666666666666666666666666667
Word: 0, Letter: З, Rate: 0.0833333333333333333333333333
Word: 0, Letter: Л, Rate: 0.0833333333333333333333333333
Word: 0, Letter: О, Rate: 0.0833333333333333333333333333
Word: 0, Letter: П, Rate: 0.0833333333333333333333333333
Word: 0, Letter: У, Rate: 0.0833333333333333333333333333
Word: 1, Letter: E, Rate: 0.1818181818181818181818181818
Word: 1, Letter: L, Rate: 0.0909090909090909090909090909
Word: 1, Letter: O, Rate: 0.0909090909090909090909090909
Word: 1, Letter: А, Rate: 0.1818181818181818181818181818
Word: 1, Letter: З, Rate: 0.0909090909090909090909090909
Word: 1, Letter: Л, Rate: 0.0909090909090909090909090909
Word: 1, Letter: О, Rate: 0.0909090909090909090909090909
Word: 1, Letter: П, Rate: 0.0909090909090909090909090909
Word: 1, Letter: У, Rate: 0.0909090909090909090909090909
Word: 2, Letter: L, Rate: 0.1818181818181818181818181818
Word: 2, Letter: W, Rate: 0.1818181818181818181818181818
Word: 2, Letter: А, Rate: 0.3636363636363636363636363636
Word: 2, Letter: Н, Rate: 0.0909090909090909090909090909
Word: 2, Letter: Р, Rate: 0.1818181818181818181818181818