def clamp(a, x, b): """ Restrict x to lie in the range [a, b] """ return max(a, min(x, b))