fonction in python

Georg Brandl g.brandl-nospam at gmx.net
Tue Jul 4 06:52:08 EDT 2006


aliassaf wrote:
> Hello, 
>   
> If we write = x^2  and if I give to the program the values of x, it will
> going to calculate the values of y, and also for x.   
> 
> But it is possible ? that is if I give to the program the values of X and Y,
> it will indicate to me the relation between the two variables, in the other
> hand if I look to the program x=2 y=4, x=3 y=9 ect... it is going to show me
> that f (t)!!! 

That is not possible at all. There are too many possible functions mapping
2 to 4, 3 to 9 etc.

You can, however, create a list of candidate functions and check your input
pairs (x,y) against each of those functions to see if any of them matches.

Short, unoptimized example:

functions = [
    lambda x: x,
    lambda x: x+1,
    lambda x: x*x,
    lambda x: x**3,
]

input = [(1,1), (2,2)]

for function in functions:
    for x,y in input:
        if function(x) != y:
            break
    else:
        print "function", function, "matches"

Georg



More information about the Python-list mailing list