【问题标题】:Inputting three integers x,y, and z to see if any operation can be made on x and y to get z as a result输入三个整数 x,y,z 看看是否可以对 x 和 y 进行任何操作得到 z 作为结果
【发布时间】:2022-01-04 00:56:28
【问题描述】:

是否有一个函数 python 可以对两个变量执行所有类型的数学函数?

【问题讨论】:

  • 单一功能,。特别是因为所有类型的数学函数都相当广泛……
  • 我认为 z * abs(x+y+0.5)^0 === z 对于 x、y 和 z 的所有整数值

标签: python python-3.x reactive-programming


【解决方案1】:

可能是这样的吗?

x, y, z = 6, 7, 42

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

该示例找到两个操作:

__mul__
__rmul__

【讨论】:

    【解决方案2】:

    这是一个处理加法、减法、乘法、除法、指数和模数的方法:

    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
    

    【讨论】:

      猜你喜欢
      • 2013-02-26
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      相关资源
      最近更新 更多