Advertisement
optybg

Untitled

Apr 30th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Worm_Ipsum
  8. {
  9.     class WormIpsum
  10.     {
  11.         static void Main()
  12.         {
  13.  
  14.  
  15.             var input = Console.ReadLine();
  16.  
  17.             while (input != "Worm Ipsum")
  18.             {
  19.                 var sentence = input.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
  20.  
  21.                 var isCapitalChar = Char.IsUpper(sentence[0][0]);
  22.  
  23.                 if (!(sentence.Length == 1 && isCapitalChar))
  24.                 {
  25.                     input = Console.ReadLine();
  26.                     continue;
  27.                 }
  28.                 var sent = sentence[0];
  29.                 var shit1 = sent.IndexOf('\'');
  30.                 var shit2 = sent.IndexOf(',');
  31.  
  32.                 var wordsInSentence = sentence[0].Split(new[] { ' ' , '\'', ','}, StringSplitOptions.RemoveEmptyEntries).ToList();
  33.  
  34.                // foreach (var word in wordsInSentence)
  35.                   for (int i = 0; i < wordsInSentence.Count; i++)
  36.                     {
  37.                     var word = wordsInSentence[i];
  38.                     Dictionary<char, int> dict = new Dictionary<char, int>();
  39.  
  40.                     int max = 0;
  41.                     foreach (char c in word)
  42.                     {
  43.                         int j;
  44.                         dict.TryGetValue(c, out j);
  45.                         j++;
  46.                         if (j > max)
  47.                         {
  48.                             max = j;
  49.                         }
  50.                         dict[c] = j;
  51.                     }
  52.  
  53.                     var mostOccurrenceChar = dict.OrderByDescending(x => x.Value);
  54.  
  55.                     var theChar = mostOccurrenceChar.First().Key.ToString().ToCharArray();
  56.  
  57.                     //var theChar = mostOccurrenceChar.Key().First().ToString().ToCharArray();
  58.  
  59.                     var theCount = int.Parse(mostOccurrenceChar.First().Value.ToString());
  60.  
  61.                     if (theCount >=2 )
  62.                     {
  63.                         var newWord = new string(theChar[0], word.Length);
  64.                         var index = wordsInSentence.IndexOf(word);
  65.                         wordsInSentence[index] = newWord;
  66.                     }
  67.                    
  68.                     //foreach (KeyValuePair<char, int> chars in dict)
  69.                     //{
  70.                     //    if (chars.Value == max)
  71.                     //    {
  72.                     //        Console.WriteLine("{0}: {1}", chars.Key, chars.Value);
  73.                     //    }
  74.                     //}
  75.                                        
  76.                     }
  77.                 var result = string.Join(" ", wordsInSentence) + ".";
  78.  
  79.                 if (shit1 != -1)
  80.                 {
  81.                     StringBuilder sb = new StringBuilder(result);
  82.                     sb[shit1] = '\'';
  83.                     result = sb.ToString();
  84.  
  85.                 }
  86.  
  87.                 if (shit2 != -1)
  88.                 {
  89.                     StringBuilder sb = new StringBuilder(result);
  90.                     sb[shit2] = ',';
  91.                     result = sb.ToString();
  92.                     result = result.Replace(",", ", ");
  93.  
  94.                 }
  95.  
  96.                 Console.WriteLine(result);
  97.        
  98.  
  99.                 input = Console.ReadLine();
  100.             }
  101.                                    
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement