繁体   English   中英

Python的位数求和

[英]Python-Sum Of Digits Raised to Powers

def sum_powers(x1, n1, x2, n2):

    """ Computes x1^n1+x2^n2
    x1,n1,x2,n2 -- numbers
    Use only call expressions,
    Not infix operators(+,-,...) """

    return __________________________

有人知道我在这里做什么吗? 我知道我应该计算哪个总和,但是我并不真正理解老师的指示。 有人可以帮忙吗? 非常感谢。

因此,据我所知,讲师希望您使用dunder方法格式。 例如: __add____sub__等。 在您的代码中,以便您了解python如何实现这些运算符,或哪些魔术方法表示这些运算符。

Operator    Method
+   object.__add__(self, other)
-   object.__sub__(self, other)
*   object.__mul__(self, other)
//  object.__floordiv__(self, other)
/   object.__truediv__(self, other)
%   object.__mod__(self, other)
**  object.__pow__(self, other[, modulo])
<<  object.__lshift__(self, other)
>>  object.__rshift__(self, other)
&   object.__and__(self, other)
^   object.__xor__(self, other)
|   object.__or__(self, other)

示例(可以通过其他方式完成):

def sum_powers(x1, n1, x2, n2):

    """ Computes x1^n1+x2^n2
    x1,n1,x2,n2 -- numbers
    Use only call expressions,
    Not infix operators(+,-,...) """

    p1 = x1.__pow__(n1)
    p2 = x2.__pow__(n2)

    return p1.__add__(p2)

这里的文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM