Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Auto-generated code below aims at helping you parse
- * the standard input according to the problem statement.
- **/
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- class Solution
- {
- static void Main(string[] args)
- {
- string N = Console.ReadLine();
- // Write an action using Console.WriteLine()
- // To debug: Console.Error.WriteLine("Debug messages...");
- //Removing the brackets at the start and the end of the string
- N = N?.Remove(0, 1);
- N = N?.Remove(N.Length - 1, 1);
- List<int> ints = new List<int>(Array.ConvertAll(N?.Split(','), int.Parse));
- List<int> finalI = new List<int>();
- string finalRanges = "";
- ints.Sort();
- List<string> intsString = ints.ConvertAll(s => s.ToString());
- int tripleC = 0;
- for (int i = 0; i < ints.Count; i++)
- {
- if (i + 1 < ints.Count && ints[i] + 1 == ints[i + 1])
- {
- tripleC++;
- finalI.Add(ints[i]);
- }
- else
- {
- if (tripleC > 1)
- {
- // 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
- finalI.Add(ints[i]);
- finalRanges += finalI[0] + "-" + finalI[finalI.Count - 1];
- // Removing the ranges from the ints string list to add in the finalranges
- intsString.RemoveRange(intsString.IndexOf(finalI[0].ToString()), finalI.Count);
- }
- if (finalRanges != "" && finalRanges[finalRanges.Length - 1] != ',')
- finalRanges += ",";
- tripleC = 0;
- finalI.Clear();
- }
- }
- ints = intsString.ConvertAll(int.Parse);
- string[] ranges = finalRanges.Split(',');
- foreach (var str in ints)
- {
- Console.Error.Write(str + ",");
- }
- Console.Error.WriteLine(" ");
- int[] rangeInts;
- for (int i = 0; i < ranges.Length - 1; i++)
- {
- int counter = 0;
- string s1 = ranges[i].Substring(0, ranges[i].IndexOf('-'));
- string s2 = ranges[i].Substring(ranges[i].IndexOf('-') + 1);
- Console.Error.WriteLine("s1 {0} s2 {1}", s1, s2);
- rangeInts = new[]
- {
- // 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
- int.Parse(s1),
- int.Parse(s2),
- };
- for (int j = 0; j < ints.Count - 1; j++)
- {
- if (ints[j] < rangeInts[0] && ints[j + 1] > rangeInts[1])
- {
- //insert the range in between the two values it's closest too, adding i as the intsString will be
- // a different length to the ints so they will get put in a row without it
- intsString.Insert(j + 1 + i, ranges[i]);
- Console.Error.WriteLine(intsString[i]);
- break;
- }
- }
- }
- for (int i = 0; i < intsString.Count; i++)
- {
- string s = intsString[i];
- if (i < intsString.Count - 1) s += ",";
- Console.Write(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement