Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. namespace ConsoleApp
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. public class Program
  9. {
  10. public static void Main()
  11. {
  12. SortedDictionary<string, SortedDictionary<string, int>> dic = new SortedDictionary<string, SortedDictionary<string, int>>();
  13.  
  14. bool isNset = false;
  15. int n = 0;
  16. int count = 0;
  17. while (true)
  18. {
  19. string log = Console.ReadLine();
  20. if (!isNset)
  21. {
  22. isNset = true;
  23. if (IsValidDurationOrCount(log))
  24. {
  25. n = int.Parse(log);
  26. }
  27. else
  28. {
  29. Console.WriteLine("Invalid count number");
  30. isNset = false;
  31. }
  32. }
  33. else
  34. {
  35. var data = log.Split(' ');
  36. if (!AnyNonLatinLetter(data[1]) && IsValidIpAddress(data[0]) && IsValidDurationOrCount(data[2]))
  37. {
  38. if (dic.ContainsKey(data[1]))
  39. {
  40. SortedDictionary<string, int> val = dic[data[1]];
  41. if (!val.ContainsKey(data[0]))
  42. {
  43. var srL = dic[data[1]];
  44. srL.Add(data[0], int.Parse(data[2]));
  45. dic[data[1]] = srL;
  46. }
  47. else
  48. {
  49. int newDuration = val[data[0]] + int.Parse(data[2]);
  50. val[data[0]] = newDuration;
  51. dic[data[1]] = val;
  52. }
  53. }
  54. else
  55. {
  56. dic.Add(data[1], new SortedDictionary<string, int>() { { data[0], int.Parse(data[2]) } });
  57. }
  58. }
  59. count++;
  60. if (count == n)
  61. {
  62. foreach (var elements in dic)
  63. {
  64. int duration = 0;
  65. string addresses = null;
  66. var firstVal = elements.Value.First();
  67. foreach (var el in elements.Value)
  68. {
  69. duration += el.Value;
  70. if (firstVal.Key == el.Key && firstVal.Value == el.Value)
  71. {
  72. addresses += el.Key;
  73. }
  74. addresses += ", " + el.Key;
  75. }
  76. Console.WriteLine(elements.Key + ": " + duration.ToString() + " [" + addresses + "]");
  77. }
  78.  
  79. }
  80. }
  81. }
  82. }
  83. private static bool IsValidDurationOrCount(string v)
  84. {
  85. int number = int.Parse(v);
  86. return number >= 1 && number <= 1000;
  87. }
  88.  
  89. public static bool AnyNonLatinLetter(string name)
  90. {
  91. foreach (var item in name)
  92. {
  93. bool flag = IsLatinLetter(item);
  94. if (!flag)
  95. {
  96. return true;
  97. }
  98. }
  99. //is length between 1 and 20;
  100. if (!(name.Length >= 1 && name.Length <= 20))
  101. {
  102. return true;
  103. }
  104. return false;
  105. }
  106. public static bool IsLatinLetter(char c)
  107. {
  108. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
  109. }
  110.  
  111. private static bool IsValidIpAddress(string IP)
  112. {
  113. var abcd = IP.Split('.');
  114. if (abcd.Count() == 4)
  115. {
  116. foreach (var item in abcd)
  117. {
  118. int number = int.Parse(item);
  119. if (!(number >= 0 && number <= 255))
  120. {
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. return false;
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement