We can assume without lost of generality that 0<f<1.

Given e>0 choose n large enough so that 1/n < e.

Subdivide (0,1) into n open subintervals E_j= ((j-1)/n,j/n) for j=1,2,...,n and c_j be the midpoint of every E_j.

Let A_j be the pullpack of E_j, that is, A_j = f^(-1) E_j.

Note that that A_j are mutually disjoint that cover K.

Define g:K-->R by setting g(x) = c_j for x in some A_j. This is a simple well-defined function with at most n values.

For any x in K choose A_j so that x in A_j, then |f(x) - g(x)|<1/n.

Thus, sup|f(x)-g(x)|<1/n<e.

It seems that K being compact is just there for confusion and does not help the solution.