Function’s graph - a homework problem Does there exist an $f(x)$ function $R\to R$, such that every line parallel to the $x$-axis meets the function’s graph
a) exactly three times?
b) an even number of times?
For b), I have several examples, which almost work, but there is a line which is tangent to the curve, so it does not work. Please help! 


Hint: For (a), consider $f(x)$ on a single interval of $\mathbb{R}$. You could try partitioning $f(x)$ into $3$ sections over this interval. You could then repeat this in every other interval but using a different range of $y$-values. You could use a different number of partitions for (b). An example function is shown in the figure below:


This is because you want a function where every $y$ value corresponds to exactly $3$ $x$-values. But this can't be a continuous function. To see why this is the case, look at the horizontal purple line in the figure below. In order to have $f(x)$ (in blue) cross the purple line exactly $3$ times, $f(x)$ would need to change direction twice (i.e. have $2$ points of inflection, if $f(x)$ is differentiable). Hence, for $y$-values more extreme than the local maxima of $f(x)$, the function would only reach each $y$ value once.

Given that we know $f(x)$ must be discontinuous to exist at all, perhaps you could try splitting the $x$-axis into $3$ sections, and defining a part of your function on each interval, as in the figure below. Thanks to @polfosol for the inspiration for this. However, this may not work either, as $f(x)$ would need to have a value at each discontinuity (i.e. the orange and green points in the figure below). And the value at the discontinuity can't be in $\mathbb{R}$ since every value's already been previously used by the red curves!

Maybe you could also try mapping the decimal expansion of each real number to a number, in such a way that each output corresponds to exactly $3$ inputs. However, I haven't figured out whether this could be done.
