Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly check if an Integer lies within a given range [duplicate]

Tags:

swift

I have an integer x and I want to check if it lies between a given boundary / within a given range.

The straightforward approach would be

let contains = x > lowerBounds && x < higherBounds

Is there a more swifty approach to this?

like image 655
ff10 Avatar asked Jul 01 '16 14:07

ff10


People also ask

How do you check if an integer is in a given range?

ValueRange. of(minValue, maxValue); range. isValidIntValue(x); it returns true if minValue <= x <= MaxValue - i.e. within the range.

How do you check if a number lies in a range in Python?

You can check if a number is present or not present in a Python range() object. To check if given number is in a range, use Python if statement with in keyword as shown below. number in range() expression returns a boolean value: True if number is present in the range(), False if number is not present in the range.


Video Answer


1 Answers

You can create a range and check if it contains x:

let contains = (lowerBounds...upperBounds).contains(x)

e.g.:

let successful = (200..<300).contains(httpStatusCode)
like image 89
ff10 Avatar answered Sep 21 '22 12:09

ff10