If $E \subset \mathbb{R}$ is a closed subset show that there is a continuous real-valued function on $\mathbb{R}$ such that $f(x) = 0$ if and only if $x \in E$.
I've considered trying to show that the preimage of any open or closed set in $\mathbb{R}$ must be open or closed, but this didn't seem to get me anywhere.
$f(x) = \min[\text{dist}(x,E),1]$, where $\text{dist}(x,E)=\inf\{|x-y|:y\in E\}$
Based on comments, we could also more simply use
$g(x) =\inf\{|x-y|:y\in E\}$