Is $f$ constant if every point is local maximum or local minimum of $f$? Suppose $f: I \to \mathbb{R}$ where $I = (a,b)$ or $I = \mathbb{R}$, etc. Suppose that every $x$ is either a local maximum of $f$ or a local minimum of $f$. Does it follow that $f$ is a constant function?
I think it's probably easy if you know $f$ is continuous, but assume for the moment that it isn't.
 A: No. For example, let $$f(x)=\begin{cases}1 & x \in \Bbb{Q} \\ 0 & x \not \in \Bbb{Q}\end{cases}$$
A: If $f$ is not constant, simply define $f(x)=\chi_{\Bbb Q}$. Or more simply, define $f(x)$ to be any two-valued function on any subsets of $\Bbb R$.
In the above cases, not only is every point a local max/min, each point is, in fact, a global max/min. See copper.hat's answer for other instances where every point is either a local max/min, but not necessarily global.
A: Let $A \subset \mathbb{R}$, and $\alpha \in \mathbb{R}$. Then $\alpha 1_A$ is such a function.
Here is another $f(x) = \lceil x \rceil$.
It is tempting to hypothesize that an equivalent condition is that every $x$ has a neighborhood $U$ such that $|f(U)| \le 2$, but while sufficient, it is not necessary as the following example shows.
Let $g = \sum_{n=1}^\infty n 1_{(\frac{1}{n+1}, \frac{1}{n}]}$, and let $U$ be any neighborhood of $0$. Then $|f(U)| = \infty$.
A: Here is a nested interval proof, assuming continuity of $f$ everywhere:
Suppose $f(a) < f(b)$. Let $a_0$ be the largest number in $[a,b]$ with $f(a_0) = f(a)$, and let $b_0$ be the smallest number in $[a_0, b]$ with $f(b_0) = f(b)$. Then for $x \in [a_0, b_0]$ we have $f(a_0) < f(x) < f(b_0)$. Now $f$ is uniformly continuous on $[a_0, b_0]$. At stage $i$ for $i \geq 1$, find $\delta$ such that if $|x - y| < \delta$, then $|f(x) - f(y)| < (f(b_{i-1}) - f(a_{i-1}) / 2$.
Further require that $\delta \leq 1/i$ and that $(b_{i-1} - a_{i-1}) / \delta$ be an integer $> 2$. Break up $[a_{i-1}, b_{i-1}]$ into $n$ segments of equal length $\delta$. Call the left endpoints of these $c_{ij}$, where $0 \leq j \leq n-1. c_{in} = b_{i-1}$. Suppose $f(c_{ij}) \geq f(c_{i(j+1)})$ for $1 \leq j \leq n-2$. Then $$f(b_{i-1}) - f(a_{i-1}) = \sum_0^{n-1} (f(c_{i(j+1)}) - f(c_{ij})) < (f(b_{i-1}) - f(a_{i-1})) / 2 + \sum_1^{n-2} (f(c_{i(j+1)}) - f(c_{ij})) + (f(b_{i-1}) - f(a_{i-1})) / 2 \leq f(b_{i-1}) - f(a_{i-1}),$$ which is a contradiction. Therefore, $f(c_{ij}) < f(c_{i(j+1)})$ for some $j$ between $1$ and $n-2$. Thus, we can find $d_i$ and $e_i$ with $a_{i-1} < d_i < e_i < b_{i-1}$, $f(d_i) < f(e_i)$, and $e_i - d_i = \delta \leq 1/i$. Let $a_i$ be the largest number in $[d_i, e_i]$ with $f(a_i) = f(d_i)$, and let $b_i$ be the smallest number in $[a_i, e_i$] with $f(b_i) = f(e_i)$. Then for $x \in [a_i, b_i]$ we have $f(a_i) < f(x) < f(b_i)$. Also note that $a_{i-1} < a_i < b_i < b_{i-1}$ and $b_i - a_i \leq 1/i$. By the Nested Interval Theorem, $S := \bigcap^{\infty}_{i=0} [a_i,b_i] \neq \emptyset$. Since the lengths are going to $0, S = \{p\}$, where $p = \sup \{a_i\} = \inf \{b_i\}$. If $N$ is an open set $\ni p$, we can find $n$ such that $p \in (a_n, b_n) \subset N$. Then $f(a_n) < f(p) < f(b_n)$, so $p$ is neither a local minimum nor a local maximum.
