Finding the inverse of a piecewise function I am struggling with finding the inverse of a piecewise function, namely:
$f(x) = \begin{cases}
          x - 1, &  0 \le x < 1\\
           2 - x,& 1 < x \le 2 \\
          \end{cases} $
For the first case $\\$
$f: y = x - 1$ when $0 \le x < 1 \rightarrow f^{-1}: x = y + 1$ when $-1 \le x < 0$
But I find this because I only have to subtract 1 from the endpoints of the inequality. I don't have a clear intuition how to do this with $2 - x$

Suppose that $f^{-1}$ there exists, then you have $f(x)=x-1$ when $0\leqslant x<1$ then setting $y=f(x)$ we get $y=x-1$ when $0\leqslant x<1$ then $x=y+1$ when $0\leqslant y+1<1$. Hence $f^{-1}(x)=x+1$ when $-1\leqslant x<0$.Also $f(x)=2-x$ when $0<x\leqslant 2$ then setting $y=f(x)$ we get $y=2-x$ when $1<x\leqslant 2$ then $x=2-y$ when $1<2-y\leqslant 2$. Hence $f^{-1}(x)=2-x$ when $0\leqslant x<1$.
Therefore, the inverse of function $f$ is given by
$$\boxed{f^{-1}: x\mapsto \begin{cases}x+1, \quad \text{if} \quad  -1\leqslant x<0,\\ 2-x, \quad \text{if} \quad 0\leqslant x<1 \end{cases}}$$
Now check $(f^{-1}\circ f)(x)=x$ for all $x\in {\rm Dom}(f^{-1})$ and $(f\circ f^{-1})(x)=x$ for all $x\in {\rm Dom}(f)$.
