Fill collection with closest values, tricky logic

G

Guest

Hi

Have these to work with.

int counter = 0; //The total item to fill the collection with
int increment = 2; //Any number
int current = 244; //Quantity
int max = 1290; //Max Quantity
int min = 0; //Min Quantity, is 0 or the increment value
List<int> list = new List<int>(); //A simple collection

What I wanna do is to fill list with the 10 closest values to the current
value.

E.g:
If current is 144 and increment is 2. list should then contain:
134, 136, 138, 140, 142, [144], 146, 148, 150, 152, 154

If current is 4 and increment is 2. list should then contain:
2, [4], 6, 8, 10, 12, 14, 16, 18, 20, 22

The value can not be less then min or more then max.

If current is 144 and increment is and max is 150. list should then contain:
130, 132, 134, 136, 138, 140, 142, [144], 146, 148, 150

If current is 144 and increment is and min is 140 and max is 150. list
should then contain:
140, 142, [144], 146, 148, 150
 
F

Fredo

What exactly is your question? Are you asking someone to write your homework
code for you? Because if that's it, you probably won't get that here. If you
have a specific question, someone will probably be happy to answer it.

Pete
 
N

Nicholas Paldino [.NET/C# MVP]

Oh how I love doing people's homework for them...

// We want to get 5 items on each side, so multiply that by the increment,
and subtract that from
// the current to get the start point. This is the same for the end point
as well.
int start = current - (5 * increment);
int end = current + (5 * increment);

// Keep track of the leftover iterations on the left and the right.
int leftLeftoverIterations = 0;
int rightLeftoverIterations = 0;

// Cycle from start to end.
for (int index = start; index <= end; index += increment)
{
// If this number is less than the min, then add to the left iterations.
if (index < min)
{
// Increment the leftover iterations.
leftLeftoverIterations++;
}
else
{
// Check to see if the number is greater than the max. If so, then
// increment the right leftover iterations.
if (index > max)
{
// Increment the right.
rightLeftoverIterations++;
}
else
{
// Add the number to the list.
list.Add(index);
}
}
}

// If there are leftover left and right iterations, then
// do nothing. Otherwise, check to see which one has extra iterations
// that need to be inserted and do so.
if (leftLeftoverIterations > 0 && rightLeftoverIterations == 0)
{
// Cycle for those iterations until the min is hit.
for (int index = 0; index < leftLeftoverIterations; ++index)
{
// Take the number at the first index and insert the next number
// in the chain down. First, calculate it.
int newNumber = list[0] - increment;

// If the new number is less than the min, then break.
if (newNumber < min)
{
// Break.
break;
}

// Insert the number at the beginning.
list.Insert(0, newNumber);
}
}

// Do the same thing for the max iterations now.
if (rightLeftoverIterations > 0 && leftLeftoverIterations == 0)
{
// Cycle for those iterations until the max is hit.
for (int index = 0; index < rightLeftoverIterations; ++index)
{
// Get the new number.
int newNumber = list[list.Count - 1] + increment;

// If the new number is greater than the max, then break.
if (newNumber > max)
{
// Break.
break;
}

// Insert the new number at the end.
list.Add(newNumber);
}
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Senna said:
Hi

Have these to work with.

int counter = 0; //The total item to fill the collection with
int increment = 2; //Any number
int current = 244; //Quantity
int max = 1290; //Max Quantity
int min = 0; //Min Quantity, is 0 or the increment value
List<int> list = new List<int>(); //A simple collection

What I wanna do is to fill list with the 10 closest values to the current
value.

E.g:
If current is 144 and increment is 2. list should then contain:
134, 136, 138, 140, 142, [144], 146, 148, 150, 152, 154

If current is 4 and increment is 2. list should then contain:
2, [4], 6, 8, 10, 12, 14, 16, 18, 20, 22

The value can not be less then min or more then max.

If current is 144 and increment is and max is 150. list should then
contain:
130, 132, 134, 136, 138, 140, 142, [144], 146, 148, 150

If current is 144 and increment is and min is 140 and max is 150. list
should then contain:
140, 142, [144], 146, 148, 150
 
G

Guest

If you don't know how to do something, then ask, and learn from it. Learning
by example.
So a big thank you to you Nicholas for helping me out.


Nicholas Paldino said:
Oh how I love doing people's homework for them...

// We want to get 5 items on each side, so multiply that by the increment,
and subtract that from
// the current to get the start point. This is the same for the end point
as well.
int start = current - (5 * increment);
int end = current + (5 * increment);

// Keep track of the leftover iterations on the left and the right.
int leftLeftoverIterations = 0;
int rightLeftoverIterations = 0;

// Cycle from start to end.
for (int index = start; index <= end; index += increment)
{
// If this number is less than the min, then add to the left iterations.
if (index < min)
{
// Increment the leftover iterations.
leftLeftoverIterations++;
}
else
{
// Check to see if the number is greater than the max. If so, then
// increment the right leftover iterations.
if (index > max)
{
// Increment the right.
rightLeftoverIterations++;
}
else
{
// Add the number to the list.
list.Add(index);
}
}
}

// If there are leftover left and right iterations, then
// do nothing. Otherwise, check to see which one has extra iterations
// that need to be inserted and do so.
if (leftLeftoverIterations > 0 && rightLeftoverIterations == 0)
{
// Cycle for those iterations until the min is hit.
for (int index = 0; index < leftLeftoverIterations; ++index)
{
// Take the number at the first index and insert the next number
// in the chain down. First, calculate it.
int newNumber = list[0] - increment;

// If the new number is less than the min, then break.
if (newNumber < min)
{
// Break.
break;
}

// Insert the number at the beginning.
list.Insert(0, newNumber);
}
}

// Do the same thing for the max iterations now.
if (rightLeftoverIterations > 0 && leftLeftoverIterations == 0)
{
// Cycle for those iterations until the max is hit.
for (int index = 0; index < rightLeftoverIterations; ++index)
{
// Get the new number.
int newNumber = list[list.Count - 1] + increment;

// If the new number is greater than the max, then break.
if (newNumber > max)
{
// Break.
break;
}

// Insert the new number at the end.
list.Add(newNumber);
}
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Senna said:
Hi

Have these to work with.

int counter = 0; //The total item to fill the collection with
int increment = 2; //Any number
int current = 244; //Quantity
int max = 1290; //Max Quantity
int min = 0; //Min Quantity, is 0 or the increment value
List<int> list = new List<int>(); //A simple collection

What I wanna do is to fill list with the 10 closest values to the current
value.

E.g:
If current is 144 and increment is 2. list should then contain:
134, 136, 138, 140, 142, [144], 146, 148, 150, 152, 154

If current is 4 and increment is 2. list should then contain:
2, [4], 6, 8, 10, 12, 14, 16, 18, 20, 22

The value can not be less then min or more then max.

If current is 144 and increment is and max is 150. list should then
contain:
130, 132, 134, 136, 138, 140, 142, [144], 146, 148, 150

If current is 144 and increment is and min is 140 and max is 150. list
should then contain:
140, 142, [144], 146, 148, 150
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top