Let $f:E\to\mathbb{R}$ be continuous, $E$ closed. Show $\exists g:\mathbb{R} \to \mathbb{R}$, where $g|_{E} = f$, g continuous. Let $E$ be a closed set of real numbers, and $f: E \to \mathbb{R}$ be continuous.  I need to show that there exists a continuous function $g: \mathbb{R} \to \mathbb{R}$ such that $g|_{E} = f$. I was given the hint to take $g$ to be linear on each of the intervals of which $\mathbb{R}\backslash E$ is composed.
This is what I did so far:

Suppose $f: E \to \mathbb{R}$ is continuous and $E$ is a closed set in $\mathbb{R}$ Since $E$ is closed, its complement $\mathbb{R} \backslash E$ must be open. Thus, $\mathbb{R} \backslash E$ can be expressed as the disjoint union of open intervals.
Suppose also, WLOG (Can I say this is WLOG?), that $\displaystyle \mathbb{R} = \left(\cup_{n\in\mathbb{N}}(a-n,a) \right)\cup E \cup \left( \cup_{n \in \mathbb{N}}(b, b+n)\right)$.
Then, we can define $g(x) = \begin{cases} f(x), & x\leq a;\\f(a), & a \leq x \leq b;\\ f(b), & x\geq a \\ \end{cases}$
Then, for both $x < a$ and $x > b$, $g$ is constant, and so continuous. And for $a<x<b$, $g = f$, and so is continuous (since $f$ is continuous on E).
The only places where we might run into trouble are $x = a$ and $x = b$, so we check them:

*

*At $x=a$, $\lim_{x \to a^{-}}g(x)=\lim_{x\to a^{-}}f(a) = f(a)$( since $g = f(a)$ constantly there), and $\lim_{x \to a^{+}}g(x) = \lim_{x \to a^{+}} f(x) = f(a)$ (since $f$ is continuous).

*At $x = b$, $\lim_{x \to b^{+}}g(x) = \lim_{x \to b^{+}}f(b) = f(b)$ (since $g = f(b)$ constantly there), and $\lim_{x \to b^{-}}g(x) = \lim_{x \to b^{-}}f(x) = f(b)$ (since $f$ is continuous).
Since, therefore $g$ is continuous at every point in $\mathbb{R}$, and coincides with $f$ on $E = [a,b]$ (again, I want to be able to say that $E = [a,b]$, but I understand that not every closed set in $\mathbb{R}$ is an interval. I don't know if, in light of the existence of the Cantor set, if there exist any results characterizing closed sets in $\mathbb{R}$ as any kind of set-theoretic combination of closed intervals?), this $g$ is the function we seek.


There's a couple of problems here, though: the hint said "linear" while I used a constant function. Now, granted, constant functions are linear after a fashion, I'm not sure this is what I was supposed to do.
Also, I saw a solution to this problem where they let $\mathbb{R}\backslash E = $ a collection of finite open intervals $(a_{k},b_{k})$ and if needed, also the open rays $(-\infty,a)$ and $(b,\infty)$, and on each $(a_{k},b_{k})$, they defined $g$ to be $g(x) = f(a_{k})+\displaystyle \frac{x-a_{k}}{b_{k}-a_{k}}(f(b_{k})-f(a_{k}))$. I don't really understand why they did that. The text of the solution can be found here (Exercise 4.5)
Is what I did sufficient, or do I need to define something like they did, and if so, could somebody please explain to me what is going on in that solution?
 A: Your assumptions on $\mathbb R\setminus E$ are incorrect. What we do have, as in the other solution you saw, is that $\mathbb R\setminus E$ is a countable disjoint union of intervals:
$$\mathbb R\setminus E=\bigcup_jI_j\qquad\text{where }I_j=(a_j,b_j)$$
where we allow the value $a_j=-\infty$ and $b_j=\infty$. This is because $E$ is closed if and only if $\mathbb R\setminus E$ is open (in fact, this is how we define closed sets) and it is a well-known result that every open set in $\mathbb R$ can be expressed as above. Note that $f(a_j)$ and $f(b_j)$ are defined for each $j$ such that $I_j$ is bounded, so we can define
$$\tilde f(x)=\begin{cases}
f(x)&\text{if }x\in E,\\
f(a_j)+\frac{f(b_j)}{b_j-a_j}(x-a_j)&\text{if }x\in I_j, I_J\text{ bounded},\\
f(b_j)&\text{if }x\in I_j,a_j=-\infty,\\
f(a_j)&\text{if }x\in I_j,b_j=\infty.
\end{cases}$$
This clearly extends $E$. Moreover, $\tilde f$ is continuous on each $I_j$ and at each interior point of $E$. The only points we need to check are the boundary points, which are the finite $a_j$ and $b_j$. One can easily check that
$$\lim_{x\to a_j^+}\tilde f(x)=f(a_j)=\tilde f(a_j),\qquad\lim_{x\to b_j^-}\tilde f(x)=f(b_j)=\tilde f(b_j)$$
(in fact, $\tilde f$ was defined on $I_j$ exactly so this would work). If $(a_j-\epsilon,a_j)\subset E$ for some $\epsilon>0$, then the fact that $f$ is continuous on $E$ implies $\lim_{x\to a_j^-}\tilde{f}(x)=\tilde{f}(a_j)$. If not, then $a_j=b_k$ for some $k\neq j$ so our above calculation shows $\lim_{x\to a_j^-}\tilde f(x)=\tilde f(a_j)$. A similar argument shows $\lim_{x\to b_j^+}f(x)=f(b_j)$, and hence $f$ is continuous everywhere.
The reason your function won't work is, as pointed out in comments, a constant function won't bridge the potential jumps. So if I define $f(x)=-3$ for $x\le0$ and $f(x)=2$ for $x\ge1$, to get a continuous extension we need to increase from $-3$ to $2$ as we go from $x=0$ to $x=1$. The simplest way to do this by linear interpolation (which is what we have done here) but there are many other functions that will do the trick.
A: A not completely nonconstructive solution is to appeal to Tietze's extension theorem, and use that metrizable spaces are normal.

Theorem Let $X$ be a topological space. If $X$ is normal, for every closed set $A\subseteq X$ and every continuous function $f:A\longrightarrow \Bbb R$ there exists a continuous extension $\hat f:X\longrightarrow \Bbb R$ of $f$ to all of $X$. 

