Discontinuous at infinitely many points While doing a worksheet on real analysis I came across the following problem.
$Q$. Let $f$ be a function defined on $[0,1]$ with the following property.
For every $y \in R$, either there is no $x$ in $[0,1]$ for which $f(x)=y$ or there are exactly two values of $x$ in $[0,1]$ for which $f(x)=y$.
(a) Prove that $f$ cannot be continuous on $[0,1]$.
(b) Construct a function $f$ which has the above property.
(c) Prove that any such function with this property has infinitely many discontinuous on $[0,1]$.
I really have absolutely no idea how to solve the problem. Even constructing the function is proving pretty difficult. Any help would be appreciated asap.
 A: Since it is a work-sheet problem, so it is better to give some hints rather than a complete solution.
So for 1st part, prove by contradiction. Looking on a small neighbor hood of minima/maxima and there use the property of continuity i.e intermediate value theorem.
For 2nd part, try to break $[0,1]$ into disjoint set of parirs.
For part 3, again try to prove it by contradiction. Assume that there is finitely many such discontinuity. And delete those points and  their conjugate points and also $\{0,1\}$ and their conjugates. Now remaining part is  union of open intervals. And we assume that the function restricted on that intervals are continuous. Now observe that in an interval if two points  attain same value then there exists a minima or maxima, and corresponde to this, in its conjugate, there exists a maxima or minima. Also observe that inside an interval either maxima exists or minima exists (Hints if in the interval $I_1=(a_1,b_1)$, $f(t_1)$ is the minima and $f(t_2)$ is the maxima, then $lt_{a\to b_1^-}f(a)$ exists. And there is a point $t_0\in I_1$ such that $lt_{a\to b_1^-}f(a)= f(t_0)$. Now look at the conjugate point $t_0'$ of $t_0$. Then $t_0'$ is a point of discontinuity, but then both it and its conjugate $t_0\in I_1$ would have been eliminated earlier, contradiction ). So we can pair up these kind of intervals one whose maxim/minima is same as others minima/maxima. And in the other case if in an interval it does not take same value at two points then there exists another interval where the function taking same values(WHY? use I.V.T and openness). So basically from here you can actually pair up intervals. And there cannot b any other kind of intervals. So number of total intervals are even. So now those eliminating points are precisely the boundary points of these intervals. Since there are even number of intervals, so their total boundary points are odd. But  initially we deleted even number of points. So it is a contradiction.  
Since an example is not included, I would like to add it to the answer since I got one after some thinking (This is the person who gave the question). One of the easy examples as an answer to ($b$) is by defining the function 
$$\begin{align} f(x)&= x^2(1+x^2) \space\space\space\space for \space x\in \mathbb Q \\&=x(1+x) \space\space\space\space\space\space\space for \space x\in \mathbb Q^{C} \end{align}$$
A: To construct a function satisfying your condition, we first construct such a function $f$ on $\Bbb R$. 

Decompose $\Bbb R$ into union of $[n,n+1)$, construct a function $f$ whose restriction $f_n:[n,n+1)\to[n,n+1)$ is defined by
  $$\begin{align}
f_n(x) &= x\quad\text{;}\quad n\le x<n+\frac 12 \\
&= x-\frac 12\quad\text{;}\quad n+\frac 12 \le x <n+1
\end{align}$$
  It is not hard to verify that $f$ has the property you want (but the domain is $\Bbb R$, however). Now get any homeomorphism $h:\Bbb R\to (0,1)$ and compose it with $f$. 

The function $\bar f:[0,1]\to \Bbb R$ satisfying the condition is construct by assigning any number not in $\mathcal R(f)$ to the image of $\bar f(0)=\bar f(1) $ and
$$\bar f:=h\circ f$$
otherwise. For the sake of completeness, you can let $h=\frac 1{1+e^{x}}$.
To prove $(c)$ see Mr. Anubhav's answer.
A: For a fairly simple concrete example for (b), for $a<b$ let 
$$f_{a,b}:(a,b]\to\Bbb R:x\mapsto\begin{cases}
x-a,&\text{if }a<x\le\frac12(a+b)\\
\frac12(b-a)-x,&\text{if }\frac12(a+b)\le x\le b\;.
\end{cases}$$
The graph consists of the straight line segment of slope $1$ from $\langle a,0\rangle$ to $\left\langle\frac12(a+b),\frac12(b-a)\right\rangle$ and the straight line segment of slope $-1$ from there to $\langle b,0\rangle$, not including the point $\langle a,0\rangle$.
Let $g_0=f_{\frac12,1}$, $g_1=f_{\frac14,\frac12}+\frac14$, $g_2=f_{\frac18,\frac14}+\frac38$, and in general let
$$g_n=f_{2^{-(n+1)},2^{-n}}+\frac12-\frac1{2^{n+1}}$$
for $n\in\Bbb N$. Finally, let
$$g:[0,1]\to[0,1]:x\mapsto\begin{cases}
g_n(x),&\text{if }2^{-(n+1)}<x\le 2^{-n}\\
0,&\text{if }x=0\;;
\end{cases}$$
then $g$ has the desired property and is discontinuous precisely at the points $x=2^{-n}$ for $n\in\Bbb Z^+$ and $x=0$. Here is a rough sketch of part of $g$:

A: Here's a simple example of such a function $\,f\colon [0,1] \to [0,\frac{1}{2}].\,$ For $x\in [0,1],$ define
$$f(x)=\begin{cases}2x,&\text{if }x \lt \frac{1}{2}\text{ and }x\text{ is the reciprocal of a power of $2,$}
\\\\x,&\text{if }x\lt\frac{1}{2}\text{ and }x\text{ is not the reciprocal of a power of $2,$}
\\\\x-\frac{1}{2},&\text{if }x \ge \frac{1}{2}.
\end{cases}$$
Let $A=\lbrace \frac{1}{2^{n+1}} \mid n\ge 1\rbrace \subseteq [0,\frac{1}{2}),$ and let $B=\lbrace \frac{1}{2^n}\mid n\ge 1\rbrace \subseteq [0,\frac{1}{2}].$
For $n\ge 1,$ $\,f(\frac{1}{2^{n+1}})= \frac{1}{2^n},\,$ so:
$$f\text{ maps }\,A\,\text{ one-one onto }\,B.$$
Also, $\,f$ is the identity on $\,[0,\frac{1}{2}) \setminus A, \,$ which equals $\, [0,\frac{1}{2}]\setminus B,\,$ so we have:
$$f\text{ maps }\;[0,\frac{1}{2}) \setminus A\;\text{ one-one onto }\;[0,\frac{1}{2}]\setminus B.$$
As a result, $$f\text{ maps }\;[0,\frac{1}{2})\;\text{ one-one onto }\;[0,\frac{1}{2}].$$
We also know that $$f\text{ maps }\,[\frac{1}{2},1]\,\text{ one-one onto }\,[0,\frac{1}{2}]$$ since $\,f(x)=x-\frac{1}{2}\,$ on $\,[\frac{1}{2},1].$
So the range of $f$ is precisely $[0,\frac{1}{2}],\,$ and for each $y$ in the range of $f,$ there are exactly two values for $x$ which will make $f(x)=y\!:$ one in $[0,\frac{1}{2})$ and the other in $[\frac{1}{2},1].$
A: This example shows that the image of $f$ can be the whole of $\mathbb{R}$:
\begin{align*}
f(0) & = 0, \\
f(x) & = \begin{cases}
\frac{3}{2^n} - 8x &
   \left( \frac{1}{2^{n + 1}} \leqslant x < \frac{5}{2^{n + 3}} \right) \\
\frac{1}{2^{n - 2}} - 8x &
   \left( \frac{5}{2^{n + 3}} \leqslant x < \frac{3}{2^{n + 2}} \right) \\
8x - \frac{5}{2^n} &
   \left( \frac{3}{2^{n + 2}} \leqslant x < \frac{7}{2^{n + 3}} \right) \\
8x - \frac{3}{2^{n - 1}} &
   \left( \frac{7}{2^{n + 3}} \leqslant x < \frac{1}{2^n} \right),
\end{cases}
\\
f(x) & = \begin{cases}
\frac{1}{8x - 5} &
   \left( \frac{1}{2} \leqslant x < \frac{5}{8} \right) \\
\frac{1}{8x - 6} &
   \left( \frac{5}{8} \leqslant x < \frac{3}{4} \right) \\
\frac{1}{7 - 8x} &
   \left( \frac{3}{4} \leqslant x < \frac{7}{8} \right) \\
\frac{1}{8 - 8x} &
   \left( \frac{7}{8} \leqslant x < 1 \right),
\end{cases} \\
f(1) & = 0.
\end{align*}
(The variable $n$ takes all positive integral values.)
\begin{alignat*}{2}
f\left(\left[\frac{1}{2^{n + 1}}, \frac{5}{2^{n + 3}}\right)\right) & =
f\left(\left[\frac{5}{2^{n + 3}}, \frac{3}{2^{n + 2}}\right)\right) && =
\left(-\frac{1}{2^{n - 1}}, -\frac{1}{2^n}\right], \\
f\left(\left[\frac{3}{2^{n + 2}}, \frac{7}{2^{n + 3}}\right)\right) & =
f\left(\left[\frac{7}{2^{n + 3}}, \frac{1}{2^n}\right)\right) && =
\left[\frac{1}{2^n}, \frac{1}{2^{n - 1}}\right),
\end{alignat*}
\begin{alignat*}{2}
f\left(\left[\frac{1}{2}, \frac{5}{8}\right)\right) & =
f\left(\left[\frac{5}{8}, \frac{3}{4}\right)\right) && =
\left(-\infty, -1\right], \\ \\
f\left(\left[\frac{3}{4}, \frac{7}{8}\right)\right) & =
f\left(\left[\frac{7}{8}, 1\right)\right) && =
\left[1, \infty\right).
\end{alignat*}
Amusingly, $f$ is continuous at $0$.
