473,659 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fill collection with closest values, tricky logic

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<intlist = 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
Jul 19 '06 #1
3 2078
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

"Senna" <Se***@discussi ons.microsoft.c omwrote in message
news:1B******** *************** ***********@mic rosoft.com...
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<intlist = 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

Jul 19 '06 #2
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 leftLeftoverIte rations = 0;
int rightLeftoverIt erations = 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.
leftLeftoverIte rations++;
}
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.
rightLeftoverIt erations++;
}
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 (leftLeftoverIt erations 0 && rightLeftoverIt erations == 0)
{
// Cycle for those iterations until the min is hit.
for (int index = 0; index < leftLeftoverIte rations; ++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 (rightLeftoverI terations 0 && leftLeftoverIte rations == 0)
{
// Cycle for those iterations until the max is hit.
for (int index = 0; index < rightLeftoverIt erations; ++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(newNum ber);
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Senna" <Se***@discussi ons.microsoft.c omwrote in message
news:1B******** *************** ***********@mic rosoft.com...
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<intlist = 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

Jul 20 '06 #3
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 [.NET/C# MVP]" wrote:
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 leftLeftoverIte rations = 0;
int rightLeftoverIt erations = 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.
leftLeftoverIte rations++;
}
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.
rightLeftoverIt erations++;
}
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 (leftLeftoverIt erations 0 && rightLeftoverIt erations == 0)
{
// Cycle for those iterations until the min is hit.
for (int index = 0; index < leftLeftoverIte rations; ++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 (rightLeftoverI terations 0 && leftLeftoverIte rations == 0)
{
// Cycle for those iterations until the max is hit.
for (int index = 0; index < rightLeftoverIt erations; ++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(newNum ber);
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Senna" <Se***@discussi ons.microsoft.c omwrote in message
news:1B******** *************** ***********@mic rosoft.com...
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<intlist = 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


Jul 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2231
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need to have all the prior dates pop in. Example: User enters 2/9/05 in the date field at the top being a Wednesday...I need the dates at the bottom to be filled in automatically, respectively as 2/3/05, 2/4/05, 2/5/05, 2/6/05, 2/7/05, 2/8/05, 2/9/05...
2
3169
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the problems that I have encountered to date and the solutions (if any) that I found. http://users.adelphia.net/~brianpclab/ServerControlCollectionIssues.htm This page also has all of the source code in a compressed file that you are free to download...
30
3295
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None, 'abc', '12345') # demonstrate map's None fill-in feature The motivation is to provide a means for looping over all data elements
14
2375
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to hassel with a multi-dimensional array. Is there such an object in VB.Net? Dim obj As someCollectionObj obj.Add("parmA1", "parmA2", "parmA3") obj.Add("parmB1", "parmB2", "parmB3") ....
14
3834
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress, ZipCode, etc). So each instance of the struct will describe a person - and about 900 instances (people) will be contained in the collection. Users must be able to search for a specific person by any of the properties (e.g., LastName, FirstName,...
2
4199
by: slinky | last post by:
I'm getting a error when I open my . aspx in my browser... line 34: da.Fill(ds, "Assets") Here's the error and my entire code for this .aspx.vb is below that ... I need some clues as to what is causing the error... Thanks!!! Server Error in '/' Application. ---------------------------------------------------------------------------­----- Multiple-step OLE DB operation generated errors. Check each OLE DB
22
4710
by: Steve Richter | last post by:
Does the .NET framework provide a class which will find the item in the collection with a key which is closest ( greater than or equal, less than or equal ) to the keys of the collection? ex: collection keys are 20, 30, 40, 50, 60, 70, 80 find key which is >= 35. would return the 30 key.
0
1749
by: dprjessie | last post by:
Hello, I am a Web programmer and I'm working on my first desktop application as a favor for a friend. I'm sure I have a stupid error here, but there is no error being thrown so I can't figure out what is wrong. I have code that reads an excel file and fills a datagridview and it works just fine. It is also supposed to fill a database though and that is not working. The executeNonQuery command is returning a 1 which as I understand, means it has...
5
2845
by: p309444 | last post by:
Hi. I have an application in which the user can select a location and view it's distance from several Points Of Interests (POIs). When I retrieve these distances, I would also like to retrieve the ID's of the locations that are the next closest and the next furthest away from each POI. eg. If we have 10 locations, each of them a mile further away from a certain POI the I would like to return: The name of the POI, The distance from that...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.