Created
September 17, 2017 02:54
-
-
Save Aravin/2f56a0f30d77c7c77644ec3d64d86880 to your computer and use it in GitHub Desktop.
C# Program to group the sequence of number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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