Chained Comparison Check

Medium ⏱ 12 min 62% acceptance ★★★★☆ 4.3
Write a function in_range(low, value, high) that returns True if value lies between low and high inclusive, using a single chained comparison expression (e.g. low <= value <= high), not two separate and-joined comparisons written with and.

Examples

Example 1
Input
in_range(1, 5, 10)
Output
True
Explanation

1 <= 5 <= 10 holds.

Example 2
Input
in_range(1, 15, 10)
Output
False
Explanation

15 is above the upper bound.

Constraints

  • All arguments are numbers.
  • Bounds are inclusive on both ends.

Topics

Variables & Data Typeschained comparisons

Companies

JPMorgan ChaseGoldman Sachs

Hints

Hint 1

Python lets you write `a <= b <= c` directly.

Hint 2

This is equivalent to (but shorter than) `a <= b and b <= c`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.