Question

I am trying to generate numbers like this:

GetAllNumbersFrom(1, Equality.GreaterThan, 100, ",", 10)

would generate

1, 11, 21, 31, 41, 51, 61, 71, 81, 91,

The function that wrote is similar to this:

public static List<string> GetAllNumbersFrom(int i, Equality equality, int limit, string appendBy, int incrementBy = 5)
    {
        var greaterThanGivenOrEqualTo = new List<string>();
        var smallerThanGivenOrEqualTo = new List<string>();

        if (equality == Equality.GreaterThanOrEqualTo)
        {
            for (var count = i; count <= limit; count = count + incrementBy)
            {
                greaterThanGivenOrEqualTo.Add(count + appendBy);
            }
        }
        else if (equality == Equality.LesserThanOrEqualTo)
        {
            for (var count = i; count >= limit; count = count - incrementBy)
            {
                smallerThanGivenOrEqualTo.Add(count + appendBy);
            }
        }
        else if (equality == Equality.GreaterThan)
        {
            for (var count = i; count < limit; count = count + incrementBy)
            {
                greaterThanGivenOrEqualTo.Add(count + appendBy);
            }
        }
        else
        {
            for (var count = i; count > limit; count = count - incrementBy)
            {
                smallerThanGivenOrEqualTo.Add(count + appendBy);
            } 
        }

        return equality == Equality.GreaterThanOrEqualTo
                   ? greaterThanGivenOrEqualTo
                   : equality == Equality.GreaterThan
                         ? greaterThanGivenOrEqualTo
                         : smallerThanGivenOrEqualTo;
    }

But, I am sure that there must be a simpler version than I wrote. A 5 - 10 liner optimal function may be ? with out all the clumsy if loops ?

Was it helpful?

Solution

You can try following:

string GetAllNumbersFrom(int first, int last, int step, string separator)
{
    if (step == 0)
        throw new ArgumentException("zero step");
    if (Math.Sign(last - first) * Math.Sign(step) < 0)
        throw new ArgumentException("Cannot reach last in this direction");
    int count = Math.Abs((last - first) / step) + 1;
    var numbers = Enumerable.Range(0, count).Select(n => first + step * n);
    return string.Join(separator, numbers);
}

Explanation: first, we count numbers in the output list: it's distance between numbers divided by step size (and plus 1 for the start number).

Having the count, we first produce sequence 0, 1, 2, ... count-1 with Enumerable.Range. Then, we are transforming it using linear function: shift first number to the first and adjust the step.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top