Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display 1,2,3,4,5,6,8,10,11 as 1-6,8,10-11

I have this sequence 1,2,3,4,5,6,8,10,11

Expected output is 1-6,8,10-11

This problem is about formatting the sequence in easy readable form

I tried with c# and used many if & else.

Interviewer said, there is some simple algorithm to do this.

I have no idea how to achive this very simple.

Also for 1,2,3 i shown 1-3. They said its wrong!.

Is there any design pattern(interpreter) involved in this logic?

like image 429
Billa Avatar asked Feb 19 '13 08:02

Billa


2 Answers

Here is one way of doing it:

        int[] numbers = { 1, 2, 3, 4, 5, 6, 8, 10, 11 };

        int start, end;
        for (int i = 0; i < numbers.Length; i++)
        {
            start = numbers[i];

            while (i < numbers.Length - 1 && numbers[i] + 1 == numbers[i + 1])
                i++;

            end = numbers[i];

            if(start == end)
                Console.WriteLine(start);
            else
                Console.WriteLine(start + " - " + end);
        }

This will display subsequent numbers that grow incrementally as range. Numbers that are not increasing linearly are not written as part of a range.

Here is another version of the first approach, it utilizes the same for loop to iterate on range:

        int temp = numbers[0], start, end;
        for (int i = 0; i < numbers.Length; i++)
        {
            start = temp;

            if (i < numbers.Length - 1 )
                // if subsequent numbers are incremental loop further
                if (numbers[i] + 1 == numbers[i + 1])
                    continue;
                // if they are not, number at index i + 1 is a new 'start' for the next iteration
                else
                    temp = numbers[i + 1];

            end = numbers[i];

            if (start == end)
                Console.WriteLine(start);
            else
                Console.WriteLine(start + " - " + end);
        }
like image 159
Ivan Golović Avatar answered Nov 15 '22 11:11

Ivan Golović


A simple implementation in C# could look like this:

public string Format(IEnumerable<int> input)
{
    var result = string.Empty;

    var previous = -1;
    var start = -1;
    var first = true;

    foreach(var i in input)
    {
        if(start == -1)
            start = i;
        else if(previous + 1 != i)
        {
            result += FormatRange(start, previous, first);
            first = false;
            start = i;
        }

        previous = i;
    }

    if(start != -1)
        result += FormatRange(start, previous, first);

    return result;
}

public string FormatRange(int start, int end, bool isFirst)
{
    var result = string.Empty;
    if(!isFirst)
        result += ", ";
    if(start == end)
        result += start;
    else
        result += string.Format("{0}-{1}", start, end);
    return result;
}

This will also output 1-3 for the input 1,2,3, which is perfectly valid. Without a specification what the output should be instead it's impossible to answer that part.

like image 5
Daniel Hilgarth Avatar answered Nov 15 '22 11:11

Daniel Hilgarth