Reputation: 1487
I'm trying to generate a list containing range of numbers from a given min and max values.
If my min value is 14500 and max value is 58000 then I need the method to return;
Less than Rs. 20000
Rs. 20,001 - Rs. 30,000
Rs. 30,001 - Rs. 40,000
Rs. 40,001 - Rs. 50,001
Rs. 50,001 and above
My question is similar to this php question, but I'd like to make use of LINQ
if possible.
I've tried Enumerable.Range
and some code using a lot of for
and while
.
What is the best ways to do this?
Upvotes: 0
Views: 2889
Reputation: 4526
This program will take into account only the max value, because you are saying that you can accept below 6 price ranges.
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;
}
}
If you want to take into account the min value, you must provide with some logic of how it will divide the range
Upvotes: 1
Reputation: 1346
EDIT: Note that this answer returns all the numbers in the ranges. Which might not be needed. I'll leave this answer in here for those searching for a different answer.
I'm not using LINQ. But this method allows you to partition the numbers. It goes from min to first partition as in: min = 1563, partition = 100 then first list is 1563 to 1600. Next one is 1601 to 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;
}
You can force only 6 lists maximum by doing:
NumberRanges(14500, 100000, 10000).Take(6);
Giving:
Upvotes: 1
Reputation: 35780
Here could be mistakes because I am writing this in notepad but you will get the idea:
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;
}
Upvotes: 1