Home > Net >  Inputting three integers x,y, and z to see if any operation can be made on x and y to get z as a res
Inputting three integers x,y, and z to see if any operation can be made on x and y to get z as a res

Time:11-26

Is there a function python that performs all types of math functions on two variables?

CodePudding user response:

Here's one that handles addition, subtraction, multiplication, division, exponents and modulo:

def all_operations(x, y, z):
    if x   y == z: 
        return True
    if x - y == z: 
        return True
    if x * y == z:
        return True
    if x / y == z:
        return True
    if x % y == z:
        return True
    return False

CodePudding user response:

Something like this maybe?

x, y, z = 6, 7, 42

for f in dir(x):
    try:
        if getattr(x, f)(y) == z:
            print(f)
    except:
        pass

That example finds two operations:

__mul__
__rmul__
  • Related