# Finding a continuous function with specified properties

This is a homework question in my analysis class:

Let $A$ and $B$ be two nonempty closed subsets of a metric space $X$ that do no intersect. Show that there is a continuous function $f:X\rightarrow [a,b]$ such that $f(x)=a$ for all $x\in A$ and $f(x)=b$ for all $x\in B$.

Can someone give me a hint?

-
Use functions of the form $f_S(x):=\inf_{x\in S} d(x,y)$. –  Davide Giraudo Feb 25 '12 at 15:40

Why don't you start by defining $g\in [0,1]^X$ by $g(x)\equiv \frac{d(x,A)}{d(x,A)+d(x,B)}$. This function is well defined since $A$ and $B$ are closed and the two sets do not intersect. Constructing your function is now trivial.
Well you have to deal with this function $g(x)=\frac{d(x,A)}{d(x,A)+d(x,B)}$ now you consider
$h(x)=(b-a)g(x)+a$.