### Programming a function which is able to display Maximum or Minimum of an 1-dimensional function

I had to program a function today which is able to do what i wrote in the title. My attempt in doing this was that:

reset()
forget()
var('x')
Extrempkt= {}

def g(f):
f=f(x)
f1(x)=diff(f(x),x); print(f1(x))

```
l=len(solve(f1(x)==0,x))
L=l-1;
A=[((solve(f1(x)==0,x)[n]).operands()[1]) for n in [0..L]]; print(A)
if n in [0..L]:
if f1(A[n]-0.0000001)>f1(A[n]+0.0000001):
Extrempkt.update({A[n]:"Maximum"})
if f1(A[n]-0.0000001)<f1(A[n]+0.0000001):
Extrempkt.update({A[n]:"Minimum"})
return (Extrempkt)
```

g((1/9)*x^3-(1/3)*x^2-(8/3)*x+9)

The problem is. My function only puts out my 2nd Extremum.
Can you help me?