Skip to content

Instantly share code, notes, and snippets.

@Aravin
Created September 17, 2017 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aravin/2f56a0f30d77c7c77644ec3d64d86880 to your computer and use it in GitHub Desktop.
Save Aravin/2f56a0f30d77c7c77644ec3d64d86880 to your computer and use it in GitHub Desktop.
C# Program to group the sequence of number
using System;
using System.Collections.Generic;
namespace CombineSequenceNumbers
{
class Program
{
static void Main(string[] args)
{
string input;
// Getting intput
Console.WriteLine("Enter the number: ");
input = Console.ReadLine();
// Calling the method
Console.WriteLine(CombineSequenceNumber(input + ",0"));
Console.ReadKey();
}
// Method to combine the seq number
public static string CombineSequenceNumber(string ip)
{
string[] input = ip.Split(',');
List<string> output = new List<string>();
int startSeq = 0;
for (int i = 0; i < input.Length-1; i++)
{
if (startSeq == 0)
startSeq = Convert.ToInt32(input[i]);
int curVal = Convert.ToInt32(input[i]);
int nextVal = Convert.ToInt32(input[i+1]);
if ( curVal + 1 != nextVal)
{
if(startSeq == curVal)
output.Add(input[i]);
else
output.Add(startSeq + "-" + input[i]);
startSeq = 0;
}
}
// returning the output
return String.Join(",", output);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment