Advertisement
Guest User

Untitled

a guest
Jul 15th, 2017
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. public class WormIpsum
  7. {
  8. public static void Main()
  9. {
  10. var regex = new Regex(@"[A-Z]{1}[\w\W]+?\.{1,}");
  11. var currentSentence = Console.ReadLine();
  12. while (currentSentence != "Worm Ipsum")
  13. {
  14. var matched = regex.Matches(currentSentence);
  15. if (matched.Count > 1)
  16. {
  17. currentSentence = Console.ReadLine();
  18. continue;
  19. }
  20. var newSentence = ProcessAndValidate(currentSentence);
  21. Console.WriteLine($"{newSentence}.");
  22.  
  23. currentSentence = Console.ReadLine();
  24. }
  25. }
  26.  
  27. private static string ProcessAndValidate(string currentSentence)
  28. {
  29. var returnedSentence = string.Empty;
  30. var words = currentSentence.TrimEnd('.').Split(' ');
  31. var isFirst = true;
  32. foreach (var word in words)
  33. {
  34. var currentWord = FindMostOccurendLetter(word);
  35. if (isFirst)
  36. {
  37. returnedSentence += currentWord;
  38. isFirst = false;
  39. continue;
  40. }
  41. returnedSentence += $" {currentWord}";
  42. }
  43. return returnedSentence;
  44. }
  45.  
  46. static string FindMostOccurendLetter(string word)
  47. {
  48. var symbols = new Dictionary<char, int>();
  49. for (int index = 0; index < word.Length; index++)
  50. {
  51. var currentSymbol = word[index];
  52. if (!symbols.ContainsKey(currentSymbol))
  53. {
  54. symbols[currentSymbol] = 0;
  55. }
  56. symbols[currentSymbol]++;
  57. }
  58. var returnedWord = symbols.OrderByDescending(x => x.Value).First().Key;
  59. var value = symbols.OrderByDescending(x => x.Value).First().Value;
  60. if (value>= 2)
  61. {
  62. var newWord = String.Empty;
  63. for (int i = 0; i < word.Length; i++)
  64. {
  65. if (word[i]=='\'')
  66. {
  67. newWord += '\'';
  68. }
  69. else if (word[i]==',')
  70. {
  71. newWord += ',';
  72. }
  73. else
  74. {
  75. newWord += returnedWord;
  76. }
  77. }
  78. return newWord;
  79. }
  80. else
  81. {
  82. return word;
  83. }
  84.  
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement