Trying to build inverse function
<p><strong>* If you are doing intro2cs in huji be careful...
   I know why you're here *</strong></p>

<p>I'm trying to build a function that get a function (assume continuity of monotone) as argument and return its inverse function. 
From mathematics I know that I need to reflect the function to <code>y=x</code>. 
but this isn't going very well.</p>

<p>I wrote something that will give me the <code>x0</code> that makes <code>f(x0)=0</code>.</p>

<pre><code>def solve(f, x0=-10000, x1=10000, epsilon=EPSILON):
"""return the solution to f in the range between x0 and x1"""

while x1-x0&gt;EPSILON:

    if (f(x0)*f((x1+x0)/2))&lt;0:
        x1=((x1+x0)/2)
    elif (f(x0)*f((x1+x0)/2))&gt;0:
        x0=((x1+x0)/2)
    elif (f(x0)*f(x1))==0:
        if f(x0)==0:
            return x0
        else:
            return x1
if f(x0)*f(x1)&lt;0:
    return (x1+x0)/2
else:
    return None
</code></pre>

<p>The problem is that I don't know what will be the range's input on the function. 
I think I need to work on small range at first, and if I won't find a solution i'll expand the range exponentially. </p>

<p>Any ideas? </p>

<p>UPDATE: 
ok so I tried to write it as <code>@Spektre</code> suggested and I succeed: </p>

<pre><code>def inverse(g, epsilon=EPSILON):
    """return f s.t. f(g(x)) = x"""

    def helper(y):
        x0=-2
        x1=2
        while x1&lt;10000:
            if solve((lambda x:g(x)-y),x0,x1,epsilon):
                return solve((lambda x:g(x)-y),x0,x1,epsilon)
            else:
                x0=math.pow(x0,3)
                x1=math.pow(x1,3)
    return helper
</code></pre>