TITLE: Continuous functions that attain local extrema at every point
QUESTION [8 upvotes]: Let $f:[0,1]\to\mathbb R$ is a continuous function, and for all $x\in [0,1]$, $f(x)$ is either a local maximum or a local minimum. Then prove that $f$ is a constant.
Here is what I have tried, I don't know whether it is correct:
Assume $f$ is not a constant, by continuity, there exist $x_1$, $x_2$ such that $f(x_1)=m$ is a global minimum and $f(x_2)=M$ is a global maximum. Now if $m\ne M$, choose any $c\in(m,M)$ and define
$$x_0=\sup (x\in[x_1,x_2]:f(x)<c)$$
Then we have $f(x_0)$ NOT a local minimum nor a local maximum (since according to the definition of $x_0$, there are always some points $<c$ in the left neighborhood of $x_0$ and the points in right neighborhood are always $>c$). Contradiction arises and so $f$ must be a constant.

REPLY [6 votes]: One reason why I quite like the problem is that it starts with a nice magic trick (a misdirection if you like).  Since it
is posed for continuous functions one immediately starts by thinking about that, when in fact you should be simply investigating the
nature of the set of local extrema.  As others have pointed out it is just a countable vs. uncountable situation that resolves the
problem.  Continuity just jumps in at the last moment to finish the argument.
I don't have any seriously new ideas but I would like to remind readers of a standard device in real variable arguments that works quite 
simply here too.  It is worth keeping in your toolkit and has been used many, many times. So this is essentially the same proof as the others but with more of a real-variable feel, than a topological one.

Lemma.  Let $f:\mathbb{R}\to\mathbb{R}$ and let $E$ be the set of
  points at which $f$ has a local maximum or a local minimum. Then there
  is a denumerable decomposition of $E$ into a sequence of sets
  $\{E_n\}$ such that $f$ is constant on each $E_n$.

Proof.  Let $A$ be the set of points where $f$ has a local maximum.  For each $x\in A$ there is a $ \delta(x)>0$ so
that $f(z)\leq f(x)$ for all $z\in (x-\delta(x),x+\delta(x))$.
Define the collection
$$
A_{nj} = \left\{
 x\in A: \frac{1}{n} \leq \delta(x) < \frac1{n-1}
  \right\}
\cap \left[ \frac{j}n, \frac{j+1}n \right)
$$
for $n=1,2,3 \dots$ and $j=0, \pm 1, \pm 2, \pm 3, \dots$.
Suppose that $x$ and $y$ with $x<y$ belong to the same set $A_{nj}$.  Then
$$
0< y-x < \frac1n \leq \delta(x)  \implies f(y) \leq f(x)
$$
while 
$$
0< y-x < \frac1n \leq \delta(y)  \implies f(x) \leq f(y)
.$$
 Thus $f$ is constant on each set $A_{nj}$.  The same argument handles the set of points where $f$ has a local minimum.  QED
Then, in order to misdirect our easily mislead students, we pose the problem this way:

Problem.  Suppose $f:[0,1]\to\mathbb{R}$ is a continuous function and
  every point of $(0,1)$ with at most countably many exceptions is
  either a local maximum or a local minimum.  Must $f$ be constant on
  $[0,1]$?


Comments on the method.
I am by no means an historian of mathematics, but that has never stopped me from speculating on the sources of ideas in analysis.  I can trace this little technique back at least to

Beppo Levi, Richerche sulle funzioni derivate, Atti della Accademia
  Reale (Nazionale) dei Lincei, (1906).

So, as a particularly lazy historian, I am going to award the title to the Italian mathematician Beppo Levi [1875-1961] (not, it should be noted, one of the Marx brothers).
His theorem was this: 

Theorem (Levi) Suppose that   $f:\mathbb{R}\to\mathbb{R}$ has at each point $x$   of a set  $E$ a finite
  right-derivative $f_+'(x)$ and a finite left derivative $f_-'(x)$. 
  Then $f_-'(x)=f_+'(x)$ at all but countably many point of $E$.

His proof is just this (expressed similarly to the technique above).  We can analyze the set where $f_-'(x)<f_+'(x)$
 by considering the set
 $$E^r= \{x\in E:   f_-'(x)< r < f_+'(x)\}$$
 for rationals $r$.
For each $x\in E^r$ there is a $  \delta(x)>0$ so
 that $[f(z)-f(x)/[z-x]> r $ for all $z\in (x,x+\delta(x))$
 and at the same time
  $[f(z)-f(x)/[z-x]< r $ 
 for all $z\in (x-\delta(x),x)$.
Define the collection
 $$
 A_{nj} = \left\{
 x\in E^r: \frac{1}{n} \leq \delta(x) < \frac1{n-1}
 \right\}
 \cap \left[ \frac{j}n, \frac{j+1}n \right)
 $$
 for $n=1,2,3 \dots$ and $j=0, \pm 1, \pm 2, \pm 3, \dots$.
Now show that there cannot be two or more points in any set
 $ A_{nj}$.  That means that $E^r$ is countable and hence (since the rationals are countable)   the set
 $\{x\in E: f_-'(x) < f_+'(x)\}$ is countable.
A very nice simple proof but, more importantly, a technique that can be and has been used a great many times.