Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a set of numbers:

A B C D E F G
10 3 6 8 22 10 34

The above set of numbers are in a class named, 'Range':

class
{
int A;
int B;
int C;
int D;
int E;
int F;
int G;
}

I also have a list of type <range> which contains more 'Range' typed objects.

Example:

List<Range> objects = new List<Range>();


And let's say we have 2 ranges added in this list:

15, 1, 2, 10, 40, 1, 33
0, 0, 100, 0, 2, 1, 34

Now I need to find the ranges with the closest values to the Range I first mentioned, which is:

A B C D E F G
10 3 6 8 22 10 34

I need to find the range that has the closest range to the range mentioned above by comparing each Letter value to the required range's respective value. In this case, 15, 1, 2, 10, 40, 1, 33 is the best option since most of its values are closer to the values of the required range than the second option.

How can I do this folks?
Posted
Comments
Sergey Alexandrovich Kryukov 4-Apr-15 1:35am    
The problem is not completely defined. Define "closest".
given 1, 2, 3, 4, which is the closest
1, 3, 4, 4
or
1, 2, 3, 5?
Don't show example, provide strict comprehensive definition.
Anyway, what have you tried so far?
—SA

1 solution

Here is a suggestion. Subtract each field in the test Range from the corresponding field in the target Range. Take the absolute value of each result and sum them. The Range with the smallest total wins. The absolute value of x can be found using Math.Absolute(x) . A more sophisticated method would be to express the absolute value as a percentage of the maximum possible value of that field.

 
Share this answer
 
v2
Comments
Dinuka Jayasuriya 4-Apr-15 3:16am    
Yes this is what I tried. Thank you!
BillWoodruff 4-Apr-15 4:41am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900