Bijection between an open and a closed interval

It seems that your construction is fine, however coarse and crude. We usually give this question in the introductory course of set theory, the solution is quite elegant too.

Firstly, it is very clear that this function cannot be continuous. Consider a sequence approaching the ends of the interval, the function cannot be continuous there.

Secondly, without the loss of generality assume the interval is $[0,1]$. Define $f(x)$ as following: $$f(x) = \left\{ \begin{array}{1 1} \frac{1}{2} & \mbox{if } x = 0\\ \frac{1}{2^{n+2}} & \mbox{if } x = \frac{1}{2^n}\\ x & \mbox{otherwise} \end{array} \right.$$

It is relatively simple to show that this function is as needed.


Define a bijection $f:(-1,1)\rightarrow[-1,1]$ as follows: $f(x)=2x$ if $|x|=2^{-k}$ for some $k\in\mathbb{N}$; otherwise $f(x)=x$.


Here you have simpler construction.

Let $(a_n)$ be the sequence in $(a,b)$ defined by $a_n = a + \frac{b-a}{2^n}$. Then let $f\colon [a,b] \to (a,b)$ be given by $$ f(x) = \begin{cases} a_1, & x = a,\\ a_2, & x = b,\\ a_{n+2}, & x = a_n, n = 1,2,\dots\\ x, & x \in [a,b] \setminus \{a,b,a_1,a_2,\dots\}. \end{cases} $$ Of course definition of $(a_n)$ could be different. The only important thing is that it is a sequence in $(a,b)$.