I have numbers list like this: {100, 150, 200, 250, 300, 350, 400, 450}
How to find lowest and highest number in between by given number ?
Examples:
x = 90
and desired output is 100
.x = 120
then desired output is 100, 150
x = 150
then desired output is 150
x = 151
then desired output is 150, 200
x = 420
then desired output is 400, 450
x = 450
then desired output is 450
x = 451
then desired output is > 450
Code:
I have tried with windows app ( C# ), but results are not accurate.
private void GetRangeList()
{
string givenValue = txtgivenValue.Text.ToString();
long givenValueNumber = long.Parse(txtgivenValue.Text.ToString().Trim());
var numbers = new List<long> { 100, 150, 200, 250, 300, 350, 400, 450 };
var lowest = numbers.Where(n => n <= givenValueNumber).Max().ToString();
var highest = string.Empty;
if (givenValueNumber < 450)
{
highest = numbers.Where(n => n >= givenValueNumber).Min().ToString();
}
lblOutput.Text = lowest.ToString() + ", " + highest.ToString();
}
Please, note that Max()
/ Min()
on empty cursor throws exception :
long givenValueNumber = 90;
var numbers = new List<long> { 100, 150, 200, 250, 300 , 350 , 400 , 450 };
// n => n <= givenValueNumber condition makes the cursor empty
// Max() will throw exception
var lowest = numbers.Where(n => n <= givenValueNumber).Max();
This fact, probably, is the main difficuly in your case; however, one simple foreach
loop is enough. The only (small) difficulty is to return the result in the right format for all possible cases:
// IEnumerable<long> be nice and accept any enumerable data source;
// say, array, list etc.
private static string TheRange(IEnumerable<long> data, long target) {
long? lower = null;
long? upper = null;
// all we have to do is to enumerate the data while updating lower and upper
// bounds in the process
foreach (var item in data) {
if (item <= target && (!lower.HasValue || item > lower))
lower = item;
if (item >= target && (!upper.HasValue || item < upper))
upper = item;
}
// we have upper and lower bound; time to return them in the right format
if (!lower.HasValue)
if (!upper.HasValue)
return $"Empty array";
else
return $"< {upper}";
else if (!upper.HasValue)
return $"> {lower}";
else if (lower == upper)
return $"{lower}";
else
return $"{lower}, {upper}";
}
Let's have a look:
using System.Linq; // for testing only
...
var numbers = new List<long> { 100, 150, 200, 250, 300, 350, 400, 450 };
long[] tests = new long[] {
90, 120, 150, 151, 420, 450, 451
};
string report = string.Join(Environment.NewLine, tests
.Select(test => $"{test, 3} :: {TheRange(numbers, test)}"));
Console.Write(report);
Outcome:
90 :: < 100
120 :: 100, 150
150 :: 150
151 :: 150, 200
420 :: 400, 450
450 :: 450
451 :: > 450
Finally, GetRangeList()
can be implemented as follows
private void GetRangeList() {
var numbers = new List<long> { 100, 150, 200, 250, 300, 350, 400, 450 };
// 1. txtgivenValue.Text is pf type string, ToString() is redundant
// 2. Parse is smart enough to call Trim when necessary
long givenValueNumber = long.Parse(txtgivenValue.Text);
lblOutput.Text = TheRange(numbers, givenValueNumber);
}
Just use:
var belowList = numbers.Where(i => i <= x);
var aboveList = numbers.Where(i => i >= x);
long? upper = belowList.Any() ? belowList.Max() : null;
long? lower = aboveList.Any() ? belowList.Min() : null;
// now you need to handle various scenarios
if( upper.HasValue && lower.HasValue )
{
// both are found and have value
// if (upper == lower) return any
// else return both
}
else if( ! upper.HasValue )
{
// belowList was empty
string result "< " + numbers.Min();
}
else if( ! lower.HasValue )
{
// aboveList was empty
string result "> " + numbers.Max();
}
else
{
// none have value
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.