simonradev

WormIpsum

Apr 28th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. namespace WormIpsum
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Text.RegularExpressions;
  8.     using System.Threading.Tasks;
  9.  
  10.     public class Startup
  11.     {
  12.         private static StringBuilder result;
  13.         private static Dictionary<char, int> characterOccurrencesInSingleWord;
  14.  
  15.         public static void Main()
  16.         {
  17.             result = new StringBuilder();
  18.             characterOccurrencesInSingleWord = new Dictionary<char, int>();
  19.  
  20.             Regex sentenceToWorkOn = new Regex(@"([A-Z].*?\.)");
  21.  
  22.             string inputLine = Console.ReadLine();
  23.             while (inputLine != "Worm Ipsum")
  24.             {
  25.                 if (sentenceToWorkOn.Matches(inputLine).Count != 1)
  26.                 {
  27.                     inputLine = Console.ReadLine();
  28.  
  29.                     continue;
  30.                 }
  31.  
  32.                 Match match = sentenceToWorkOn.Match(inputLine);
  33.  
  34.                 string currentSentence = match.Value;
  35.                 string oldSentence = currentSentence;
  36.  
  37.                 string[] wordsFromSentence = currentSentence
  38.                                                 .TrimEnd('.')
  39.                                                 .Split(new[] { ',', '-', ' ', ':', ';', '!', '?'}, StringSplitOptions.RemoveEmptyEntries);
  40.  
  41.                 foreach (string word in wordsFromSentence)
  42.                 {
  43.                     GetTheOccurrencesOfEveryCharacterInSingleWord(word);
  44.  
  45.                     char toReplaceTheWordWith = default(char);
  46.                     if (!GetTheCharacterToReplaceWithIfOneOccurresMoreThanOnce(out toReplaceTheWordWith))
  47.                     {
  48.                         characterOccurrencesInSingleWord.Clear();
  49.  
  50.                         continue;
  51.                     }
  52.  
  53.                     string newWord = ReplaceTheWordWithTheGivenCharacter(word.Length, toReplaceTheWordWith);
  54.  
  55.                     currentSentence = currentSentence.Replace(word, newWord);
  56.  
  57.                     characterOccurrencesInSingleWord.Clear();
  58.                 }
  59.  
  60.                 inputLine = inputLine.Replace(oldSentence, currentSentence);
  61.  
  62.                 result.AppendLine(inputLine);
  63.  
  64.                 inputLine = Console.ReadLine();
  65.             }
  66.  
  67.             Console.Write(result);
  68.         }
  69.  
  70.         private static string ReplaceTheWordWithTheGivenCharacter(int lenghtOfWord, char toReplaceTheWordWith)
  71.         {
  72.             return new string(toReplaceTheWordWith, lenghtOfWord);
  73.         }
  74.  
  75.         private static bool GetTheCharacterToReplaceWithIfOneOccurresMoreThanOnce(out char toReplaceTheWordWith)
  76.         {
  77.             char bestCharacterMatch = default(char);
  78.  
  79.             int maxOccurrencesOfChar = int.MinValue;
  80.             int currentOccurrencesOfChar = 0;
  81.             foreach (KeyValuePair<char, int> kvp in characterOccurrencesInSingleWord)
  82.             {
  83.                 currentOccurrencesOfChar = kvp.Value;
  84.  
  85.                 if (currentOccurrencesOfChar > maxOccurrencesOfChar)
  86.                 {
  87.                     maxOccurrencesOfChar = currentOccurrencesOfChar;
  88.  
  89.                     bestCharacterMatch = kvp.Key;
  90.                 }
  91.             }
  92.  
  93.             toReplaceTheWordWith = bestCharacterMatch;
  94.  
  95.             return maxOccurrencesOfChar > 1;
  96.         }
  97.  
  98.         private static void GetTheOccurrencesOfEveryCharacterInSingleWord(string word)
  99.         {
  100.             foreach (char character in word)
  101.             {
  102.                 if (!characterOccurrencesInSingleWord.ContainsKey(character))
  103.                 {
  104.                     characterOccurrencesInSingleWord[character] = 0;
  105.                 }
  106.  
  107.                 characterOccurrencesInSingleWord[character]++;
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment