生成包含最小值和最大值的数字范围的列表

Generating a list containing range of numbers from min and max values

我正在尝试生成一个列表,其中包含给定最小值和最大值的数字范围。

如果我的最小值为 14500,最大值为 58000,那么我需要 return;

的方法

不到卢比。 20000
卢比20,001 - 卢比。 30,000
卢比30,001 - 卢比。 40,000
卢比40,001 - 卢比。 50,001
卢比50,001 及以上

我的问题与此类似 php question,但我想尽可能使用 LINQ

我已经尝试了 Enumerable.Range 和一些使用大量 forwhile 的代码。 执行此操作的最佳方法是什么?

这里可能有错误,因为我是用记事本写的,但你会明白的:

var start = min - min % 10000 + 10000;

list.Add(string.Format("less then {0}", start));

while(start < max)
{
    if(start + 10000 > max)
       list.Add(string.Format("{0} and above", start + 1)); 
    else   
       list.Add(string.Format("rs. {0} - rs. {1}", start + 1, start + 10000));

    start += 10000;
}

这个程序将只考虑最大值,因为你是说你可以接受低于 6 个价格范围。

class Program
{
    static void Main(string[] args)
    {

        int maxItems = 6;
        int minItems = 3;
        int maxValue = 99232;
        int minValue = 99000;

        //the list to store string formmated items
        List<string> finalList = new List<string>();

        int divider = (GetBestValue(maxValue, minValue, maxItems, minItems));

        int startingPoint = ((minValue / divider) + 1) * divider;

        finalList.Add(string.Format("less then {0}", startingPoint));

        int currentAmmount = startingPoint;

        while (currentAmmount < maxValue)
        {
            if (currentAmmount + divider > maxValue)
                finalList.Add(string.Format("{0} and above", currentAmmount + 1));
            else
                finalList.Add(string.Format("rs. {0} - rs. {1}", currentAmmount + 1, currentAmmount + divider));

            currentAmmount += divider;
        }

        foreach (var item in finalList)
        {
            Console.WriteLine(item);
        }
    }

    /// <summary>
    /// This method will seek the best divider to take 
    /// </summary>
    /// <param name="max">Max value to use</param>
    /// <param name="maxItems"></param>
    /// <param name="minValue"></param>
    /// <returns></returns>
    static int GetBestValue(int max, int min, int maxItems, int minItems) 
    {

        int currentDivider = 1;
        int currentItems;
        int startingMaxValue = max;
        int range = (max - min);

        while (startingMaxValue/10 > 0)
        {
            currentDivider *= 10;
            startingMaxValue /= 10;
        }

        //check aginst max value
        while (max / currentDivider > maxItems) 
        {
            currentDivider *= 2;
        }

        //check aginst min items

        currentItems =range / currentDivider;

        while (currentItems < minItems) 
        {
            currentDivider /= 2;
            currentItems = range / currentDivider;

        }

        return currentDivider;
    }
}

如果你想考虑最小值,你必须提供一些如何划分范围的逻辑

编辑:请注意,此答案 returns 范围内的所有数字。这可能不需要。对于那些正在寻找不同答案的人,我会在这里留下这个答案。

我没有使用 LINQ。但是这种方法允许您对数字进行分区。它从 min 到第一个分区,如下所示:min = 1563,partition = 100 然后第一个列表是 1563 到 1600。下一个是 1601 到 1700。

static void Main(string[] args)
{
    foreach (var numberRange in NumberRanges(14500, 58000, 10000))
    {
        Console.WriteLine(": {0}, {1}", numberRange.Min(), numberRange.Max());
    }
}

static IEnumerable<IEnumerable<int>> NumberRanges(int min, int max, int partitionSize)
{
    int num = min;

    List<int> numPart = new List<int>(partitionSize);
    while (num <= max)
    {
        numPart.Add(num++);
        if (num % partitionSize == 0)
        {
            numPart.Add(num++);
            yield return numPart;
            numPart.Clear();
        }
    }
    if (numPart.Any())
        yield return numPart;
}

您最多只能强制使用 6 个列表:

NumberRanges(14500, 100000, 10000).Take(6);

给予: