Measure of a function that maps rationals to $0$ and irrationals to $2^n$ 
Let $f$ be a function such that $f(x)=0$ for all $x \in \mathbb{Q}$, and $2^n$ for all $x \in \mathbb{R} \setminus \mathbb{Q}$ where $n$ is the number of zeros immediately after the decimal.
Show that this is measurable.

My first idea here was that since the irrationals are a $G_\delta$ set, they are measurable.  There are countably many rationals in $[0,1]$, so that set is also measurable.  I suppose this shows that $f$ is measurable.
 A: For each positive integer $n$, let $x_n$ be the digit in the $n^\text{th}$ position after the decimal point in the decimal expansion of $x\in\mathbb R$.  Then $x_n$ is a measurable function of $x$, because $x_n=\lfloor 10(10^{n-1}x-\lfloor 10^{n-1}x\rfloor)\rfloor$ is a combination of Borel measurable functions.  Therefore each set $\{x\in\mathbb R:x_n=0\}$ and its complement $\{x\in\mathbb R:x_n\neq0\}$ is measurable.  Note that for each nonnegative integer $n$, $f^{-1}\{2^n\}=(\mathbb R\setminus\mathbb Q)\cap \bigcap_{k=1}^{n}\{x\in\mathbb R:x_k=0\}\cap\{x\in\mathbb R:x_{n+1}\neq 0\}$ (with the convention that $\bigcap_{k=1}^0\text{stuff}=\mathbb R$).  Therefore this set is measurable.  For any $m\in\mathbb R$, $f^{-1}(m,\infty)$ is a countable union of such sets, or $\mathbb R$.  
As Arturo notes in a comment, you can also describe $f^{-1}\{2^n\}$ as a countable union of open intervals with rationals removed, and this is another way to see that it is measurable.  There was a somewhat similar question in which I gave a somewhat similar answer while Arturo again gave a more geometric approach.
A: First, notice that $f$ is well-defined because the numbers that
have two different decimal expansions are all rationals.
Let $f_n(x) = 2^k$, where $k$ is the number of zeros from the first to the $n$-th
position after the decimal.
In order to avoid the ambiguity mentioned above, let's agree
that no decimal expansion ends in an infinite sequence of $9$.
That is, no expansion ends in $99999\dotsc$.
Then, $f_n$ is measurable.
(Why? Hint: write it as a step function.)
Now, observe that
$$
  I_{\mathbb{R} \setminus \mathbb{Q}} f_n \uparrow f,
$$
where $I_{\mathbb{R} \setminus \mathbb{Q}}$ is $0$ at the rationals and
$1$ at the irrationals.
Therefore, $f$ is measurable.

Edit: fixed the definition of $f_n$.
