Advertisement
Guest User

Untitled

a guest
Feb 28th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1.  
  2. /**
  3. * Auto-generated code below aims at helping you parse
  4. * the standard input according to the problem statement.
  5. **/
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.ComponentModel;
  10. using System.Diagnostics;
  11.  
  12.  
  13. class Solution
  14. {
  15. static void Main(string[] args)
  16. {
  17. string N = Console.ReadLine();
  18. // Write an action using Console.WriteLine()
  19. // To debug: Console.Error.WriteLine("Debug messages...");
  20. //Removing the brackets at the start and the end of the string
  21. N = N?.Remove(0, 1);
  22. N = N?.Remove(N.Length - 1, 1);
  23. List<int> ints = new List<int>(Array.ConvertAll(N?.Split(','), int.Parse));
  24. List<int> finalI = new List<int>();
  25. string finalRanges = "";
  26. ints.Sort();
  27. List<string> intsString = ints.ConvertAll(s => s.ToString());
  28. int tripleC = 0;
  29. for (int i = 0; i < ints.Count; i++)
  30. {
  31. if (i + 1 < ints.Count && ints[i] + 1 == ints[i + 1])
  32. {
  33. tripleC++;
  34. finalI.Add(ints[i]);
  35. }
  36. else
  37. {
  38. if (tripleC > 1)
  39. {
  40. // Add the one after as the previous one will fail for the end of the range, example: range is 5-7,previously would print 5,6 instead of 5,6,7
  41. finalI.Add(ints[i]);
  42. finalRanges += finalI[0] + "-" + finalI[finalI.Count - 1];
  43. // Removing the ranges from the ints string list to add in the finalranges
  44. intsString.RemoveRange(intsString.IndexOf(finalI[0].ToString()), finalI.Count);
  45. }
  46.  
  47. if (finalRanges != "" && finalRanges[finalRanges.Length - 1] != ',')
  48. finalRanges += ",";
  49. tripleC = 0;
  50. finalI.Clear();
  51. }
  52. }
  53.  
  54. ints = intsString.ConvertAll(int.Parse);
  55.  
  56.  
  57. string[] ranges = finalRanges.Split(',');
  58. foreach (var str in ints)
  59. {
  60. Console.Error.Write(str + ",");
  61. }
  62. Console.Error.WriteLine(" ");
  63. int[] rangeInts;
  64. for (int i = 0; i < ranges.Length - 1; i++)
  65. {
  66. int counter = 0;
  67. string s1 = ranges[i].Substring(0, ranges[i].IndexOf('-'));
  68. string s2 = ranges[i].Substring(ranges[i].IndexOf('-') + 1);
  69. Console.Error.WriteLine("s1 {0} s2 {1}", s1, s2);
  70. rangeInts = new[]
  71. {
  72. // Finding the first and second number by substringing the start to before the hyphen, then after the hypen to the end to find the two numbers
  73. int.Parse(s1),
  74. int.Parse(s2),
  75. };
  76. for (int j = 0; j < ints.Count - 1; j++)
  77. {
  78. if (ints[j] < rangeInts[0] && ints[j + 1] > rangeInts[1])
  79. {
  80. //insert the range in between the two values it's closest too, adding i as the intsString will be
  81. // a different length to the ints so they will get put in a row without it
  82. intsString.Insert(j + 1 + i, ranges[i]);
  83. Console.Error.WriteLine(intsString[i]);
  84. break;
  85. }
  86. }
  87. }
  88.  
  89. for (int i = 0; i < intsString.Count; i++)
  90. {
  91. string s = intsString[i];
  92. if (i < intsString.Count - 1) s += ",";
  93. Console.Write(s);
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement