How to elegantly check if a number is within a range?
There are a lot of options:
int x = 30;
if (Enumerable.Range(1,100).Contains(x)) //true
And indeed basic if
more elegantly can be written with reversing order in the first check:
if (1 <= x && x <= 100) //true
Also, check out this SO post for regex options.
Notes:
LINQ solution is strictly for style points - since Contains iterates over all items its complexity is O(range_size) and not O(1) normally expected from a range check.
More generic version for other ranges (notice that second argument is count, not end):if (Enumerable.Range(start, end - start + 1).Contains(x)
There is temptation to write
if
solution without&&
like1 <= x <= 100
- that look really elegant, but in C# leads to a syntax error "Operator '<=' cannot be applied to operands of type 'bool' and 'int'"
Check if a value is within a range of numbers
You're asking a question about numeric comparisons, so regular expressions really have nothing to do with the issue. You don't need "multiple if
" statements to do it, either:
if (x >= 0.001 && x <= 0.009) {
// something
}
You could write yourself a "between()" function:
function between(x, min, max) {
return x >= min && x <= max;
}
// ...
if (between(x, 0.001, 0.009)) {
// something
}
Check to see if a value is within a range?
Assuming that the values of ID
are unique:
DT[, list(OK = 1 %in% seq(time.s, time.e)), by = ID]
giving;
ID OK
1: 1 TRUE
2: 2 TRUE
3: 3 FALSE
4: 4 FALSE
How do I determine if a value is within a range?
It is because you have declared i variable as int
and you are taking input as string
so when it is checking condition it is getting null
value in i variable
and not able to enter if block check below code
#include <stdio.h>
int main(){
int i;
printf("Value to check Interval \n");
scanf("%d",&i);
if (i>4 && i<6){
printf("%d Value is in first interval\n", i);
}
}
try compiling your code without if condition i variable will return a null value
How to find out if a number is in a list of ranges?
Simply iterate over the list, take both values and create a range from those values and check if b in range(...)
, also use enumerate
, start it from 1
and you will get in which consecutive range in the list the number is.
a = [[167772352, 167772415], [167772160, 167772223], [167772288, 167772351], [167772224, 167772255]]
b = 167772241
for index, (start, end) in enumerate(a, start=1):
if b in range(start, end + 1):
print(index)
break
You can also use a list comprehension:
a = [[167772352, 167772415], [167772160, 167772223], [167772288, 167772351], [167772224, 167772255]]
b = 167772241
index = [b in range(start, end + 1) for start, end in a].index(True) + 1
print(index)
Also note the end + 1
used in both range
s, that is because the range
doesn't include its end value so adding 1
means that the range is inclusive on both sides. Also both methods will get the index that starts from one, which is how you would usually count (1, 2, 3, 4, ...) as you have stated in your question that b
should be in the fourth range (which means that you started counting from 1)
How to check if a number is within a range in shell
If you are using Bash, you are better off using the arithmetic expression, ((...))
for readability and flexibility:
if ((number >= 2 && number <= 5)); then
# your code
fi
To read in a loop until a valid number is entered:
#!/bin/bash
while :; do
read -p "Enter a number between 2 and 5: " number
[[ $number =~ ^[0-9]+$ ]] || { echo "Enter a valid number"; continue; }
if ((number >= 2 && number <= 5)); then
echo "valid number"
break
else
echo "number out of range, try again"
fi
done
((number >= 2 && number <= 5))
can also be written as ((2 <= number <= 5))
.
See also:
- Test whether string is a valid integer
- How to use double or single brackets, parentheses, curly braces
Determine whether integer is between two other integers
if 10000 <= number <= 30000:
pass
For details, see the docs.
Related Topics
Can You Pass a Vector to a Vararg: Vector to Sprintf
Add Legend to "Geom_Bar" Using the Ggplot2 Package
Cbind Two Lists of Data.Frames to a New List
Replace Rbind in For-Loop with Lapply? (2Nd Circle of Hell)
Finding the Index of a Max Value in R
Fastest Way to Remove All Duplicates in R
How to Add My Outlook Email Signature to the Com Object Using Rdcomclient
R: Selecting First of N Consecutive Rows Above a Certain Threshold Value
How to Pass Vector to Integrate Function
How to Transpose a Tibble() in R
How to Get Discrete Factor Levels to Be Treated as Continuous
Update Rows of Data Frame in R
How to Create a Hyperlink Interactively in Shiny App
How to Rename Element's List Indexed by a Loop in R