如何为我的类构造多项式序列?

2024-04-23 11:38:50 发布

您现在位置:Python中文网/ 问答频道 /正文

对于赋值,我创建了一个类来表示多项式,方法是存储一个带有系数的列表,作为该类的数据成员。你知道吗

  • 列表的第一个元素(索引0)表示常量
  • 第二个(索引1)表示x项的系数,依此类推
  • 下一个元素代表多项式的下一次幂的系数。你知道吗

尽管我在为它创建最后一个函数时遇到困难"polySequence"

A function polySequence which takes a start, end, and step, and returns a generator. The generator will evaluate the polynomial for the value start, then start + end, and so on, up to end and yield these values one at a time. If no step is given, a step of 1 should be used (similar to the range method). As an example, if p represents the polynomial 2x + 1 (coefficient list [1,2]), the code

for val in p.polySequence(0,5):
    print(val)

应在单独的行上打印值1、3、5、7和9。你知道吗

下面是我对底部的Polysequence的实现:


Tags: andtheto元素列表forstepval
2条回答

看起来您只将第一个参数传递给了polySequence函数。您应该传递所有声明的参数(start、stop和step):

p = Polynomial(1, 2)
for val in p.polySequence(0, 5, 1):
    print(val)

访问polySequence方法的方式不正确。只有静态或类方法被这样调用。您需要实例化该类,然后调用polySequence方法。例如

obj = Polynomial(1, 2)
for res in obj.polySequence(0, 5, 1):
    print(res)

我还可以看到start变量总是在polySequence方法内部的代码中被覆盖。如果是这样,为什么要在方法中启动参数?你知道吗

相关问题 更多 >