Preservation of compact sets

Decide whether the following statement is true or false, justify the conclusion.

If $f$ is defined on $\mathbb R$ and $f(K)$ is compact whenever $K$ is compact, then $f$ is continuous on $\mathbb R.$

Does this hold based on the Preservation of Compact Sets: Let $f : A → \mathbb R$ be continuous on $A.$ If $K ⊆ A$ is compact, then $f(K)$ is compact as well. Which means if $f$ is a continuous function on a compact set $K,$ then the range set $f(K)$ is also compact.


Solution 1:

$$ f(x) = \begin{cases} 0 & \text{if } x<0, \\ 1 & \text{if } x\ge0. \end{cases} $$

This is not continuous, but for all compact sets $K$ the set $\{f(x): x\in K\}$ is compact.