Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace WormIpsum
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- public class Startup
- {
- private static StringBuilder result;
- private static Dictionary<char, int> characterOccurrencesInSingleWord;
- public static void Main()
- {
- result = new StringBuilder();
- characterOccurrencesInSingleWord = new Dictionary<char, int>();
- Regex sentenceToWorkOn = new Regex(@"([A-Z].*?\.)");
- string inputLine = Console.ReadLine();
- while (inputLine != "Worm Ipsum")
- {
- if (sentenceToWorkOn.Matches(inputLine).Count != 1)
- {
- inputLine = Console.ReadLine();
- continue;
- }
- Match match = sentenceToWorkOn.Match(inputLine);
- string currentSentence = match.Value;
- string oldSentence = currentSentence;
- string[] wordsFromSentence = currentSentence
- .TrimEnd('.')
- .Split(new[] { ',', '-', ' ', ':', ';', '!', '?'}, StringSplitOptions.RemoveEmptyEntries);
- foreach (string word in wordsFromSentence)
- {
- GetTheOccurrencesOfEveryCharacterInSingleWord(word);
- char toReplaceTheWordWith = default(char);
- if (!GetTheCharacterToReplaceWithIfOneOccurresMoreThanOnce(out toReplaceTheWordWith))
- {
- characterOccurrencesInSingleWord.Clear();
- continue;
- }
- string newWord = ReplaceTheWordWithTheGivenCharacter(word.Length, toReplaceTheWordWith);
- currentSentence = currentSentence.Replace(word, newWord);
- characterOccurrencesInSingleWord.Clear();
- }
- inputLine = inputLine.Replace(oldSentence, currentSentence);
- result.AppendLine(inputLine);
- inputLine = Console.ReadLine();
- }
- Console.Write(result);
- }
- private static string ReplaceTheWordWithTheGivenCharacter(int lenghtOfWord, char toReplaceTheWordWith)
- {
- return new string(toReplaceTheWordWith, lenghtOfWord);
- }
- private static bool GetTheCharacterToReplaceWithIfOneOccurresMoreThanOnce(out char toReplaceTheWordWith)
- {
- char bestCharacterMatch = default(char);
- int maxOccurrencesOfChar = int.MinValue;
- int currentOccurrencesOfChar = 0;
- foreach (KeyValuePair<char, int> kvp in characterOccurrencesInSingleWord)
- {
- currentOccurrencesOfChar = kvp.Value;
- if (currentOccurrencesOfChar > maxOccurrencesOfChar)
- {
- maxOccurrencesOfChar = currentOccurrencesOfChar;
- bestCharacterMatch = kvp.Key;
- }
- }
- toReplaceTheWordWith = bestCharacterMatch;
- return maxOccurrencesOfChar > 1;
- }
- private static void GetTheOccurrencesOfEveryCharacterInSingleWord(string word)
- {
- foreach (char character in word)
- {
- if (!characterOccurrencesInSingleWord.ContainsKey(character))
- {
- characterOccurrencesInSingleWord[character] = 0;
- }
- characterOccurrencesInSingleWord[character]++;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment