Python 运行时错误-代码中的错误在哪里

Python 运行时错误-代码中的错误在哪里,python,Python,我以前的问题(现在已经解决)是: 作为输入,我有一个非负整数列表,它们应该是多项式的系数。但我还想计算某个数x的多项式 例如: 如果我们有L=[2,3,1]作为输入,并且x=42我们得到2x^2+3x+1=3655 例如,我想要的是: >>>p=polynomial([2,3,1]) >>>p(O) 1 >>>p(42) >>>3655 >>>p=polynomial([2,3,1]) >>

我以前的问题(现在已经解决)是:

作为输入,我有一个非负整数列表,它们应该是多项式的系数。但我还想计算某个数x的多项式

例如:
如果我们有
L=[2,3,1]
作为输入,并且
x=42
我们得到
2x^2+3x+1=3655
例如,我想要的是:

>>>p=polynomial([2,3,1])
>>>p(O)
1 
>>>p(42)
>>>3655
>>>p=polynomial([2,3,1])
>>>p(O)
1 
>>>p(42)
>>>3655
>>>invert(3655,p)
42
代码是

def polynomial(coef):
 def poly(x):
    result = 0
    x_n = 1  
    for a in reversed(coef):
        result += a * x_n
        x_n *= x 
    return result
 return poly
我现在想做的是求逆,这意味着输入是一个单调多项式和正整数y,我想求一个整数x,使得p(x)=y,x应该只在[1,10**10]中,例如:

>>>p=polynomial([2,3,1])
>>>p(O)
1 
>>>p(42)
>>>3655
>>>p=polynomial([2,3,1])
>>>p(O)
1 
>>>p(42)
>>>3655
>>>invert(3655,p)
42
到目前为止,我得到的是一个运行时错误:

def polynomial(coef):
 def poly(x):
    result = 0
    xn = 1  
    for c in reversed(coef):
        result += c * xn
        xn *= x 
    return result
 return poly


def invert(y,p):

test=10**10

if p(2)>p(1):
    if p(test)>y:
        test=test//2 +(test%2)
        return invert(y,p)
    elif p(test)<y:
        test=test+(test//2)
        return invert(y,p)
    else:
        return test

if p(2)<p(1):
    if p(test)<y:
        test=test//2 +(test%2)
        return invert(y,p)
    elif p(test)>y:
        test=test+(test//2)
        return invert(y,p)
    else:
        return test

我做错了什么?

您的
invert
函数将永远递归,因为您从未修改传递给下一个调用的参数。您确实修改了
test
,但这对您没有任何好处,因为内部调用将拥有自己的
test
副本

有几种方法可以解决此问题。您可以将
test
作为参数传递给
invert
函数,初始值为第一次使用的默认值:

def invert(y, p, test=10**10):
    # ...

    # later, when you recurse:
    test = test // 2      # or whatever
    return invert(y, p, test)    # pass on the modified test value
另一种(可能更好)方法是放弃递归,而是使用循环。一个
while
循环似乎适合这里:

def invert(y, p):
    test = 10**10

    sign = (-1)**(p(2) < p(1))

    while True:
        if p(test) > y:
            test -= sign * (test // 2)
        elif p(test) < y:
            test += sign * (test // 2)
        else:
            return test    # this is the only case that returns
def反转(y,p):
测试=10**10
符号=(-1)**(p(2)y:
测试-=符号*(测试//2)
elif p(试验)

我让整个算法与您的原始代码保持一致(只是简化了一点)。如果多项式不是严格递增或严格递减,则该算法可能不正确。你应该在
test
中计算多项式的导数,以确定调整的方向,但我将把它留给你去弄清楚。

你的
invert
函数永远递归,因为你永远不会修改传递给下一个调用的参数。您确实修改了
test
,但这对您没有任何好处,因为内部调用将拥有自己的
test
副本

有几种方法可以解决此问题。您可以将
test
作为参数传递给
invert
函数,初始值为第一次使用的默认值:

def invert(y, p, test=10**10):
    # ...

    # later, when you recurse:
    test = test // 2      # or whatever
    return invert(y, p, test)    # pass on the modified test value
另一种(可能更好)方法是放弃递归,而是使用循环。一个
while
循环似乎适合这里:

def invert(y, p):
    test = 10**10

    sign = (-1)**(p(2) < p(1))

    while True:
        if p(test) > y:
            test -= sign * (test // 2)
        elif p(test) < y:
            test += sign * (test // 2)
        else:
            return test    # this is the only case that returns
def反转(y,p):
测试=10**10
符号=(-1)**(p(2)y:
测试-=符号*(测试//2)
elif p(试验)

我让整个算法与您的原始代码保持一致(只是简化了一点)。如果多项式不是严格递增或严格递减,则该算法可能不正确。你真的应该在
测试中计算多项式的导数,以确定调整的方向,但我会让你自己去弄清楚。

我冒昧地修复了你发布的代码的缩进请验证以下代码是否与您实际拥有的缩进有关以下代码确实返回了您所需的输出

def polynomial(coef):
    def poly(x):
        result = 0
        x_n = 1  
        for a in reversed(coef):
            result += a * x_n
            x_n *= x 
        return result
     return poly

def invert(y,p,test): # updated

    # test=10**10 # This was the problem

    # You reset 'test' for every recursive call
    # which means you will stand still without
    # any progress until the max num of allowed
    # recursive calls are reached.

    if p(2)>p(1):
        if p(test)>y:
            test=test//2 +(test%2)
            return invert(y,p,test) # updated
        elif p(test)<y:
            test=test+(test//2)
            return invert(y,p,test) # updated
        else:
            return test

    if p(2)<p(1):
        if p(test)<y:
            test=test//2 +(test%2)
            return invert(y,p,test) # updated
        elif p(test)>y:
            test=test+(test//2)
            return invert(y,p,test) # updated
        else:
            return test

p = polynomial([2,3,1])
t = 10**10
print(invert(3655,p,t))
def多项式(coef):
def多边形(x):
结果=0
x_n=1
对于反向(coef)中的a:
结果+=a*x\n
x_n*=x
返回结果
返回多边形
def反转(y、p、测试):#已更新
#测试=10**10#这就是问题所在
#您可以为每个递归调用重置“test”
#也就是说你会站着不动
#在达到允许的最大数量之前的任何进度
#达到递归调用。
如果p(2)>p(1):
如果p(试验)>y:
测试=测试//2+(测试%2)
返回反转(y、p、测试)#已更新

elif p(test)我冒昧地修复了您发布的代码的缩进请验证以下代码是否与您实际拥有的缩进有关以下代码确实返回了您所需的输出

def polynomial(coef):
    def poly(x):
        result = 0
        x_n = 1  
        for a in reversed(coef):
            result += a * x_n
            x_n *= x 
        return result
     return poly

def invert(y,p,test): # updated

    # test=10**10 # This was the problem

    # You reset 'test' for every recursive call
    # which means you will stand still without
    # any progress until the max num of allowed
    # recursive calls are reached.

    if p(2)>p(1):
        if p(test)>y:
            test=test//2 +(test%2)
            return invert(y,p,test) # updated
        elif p(test)<y:
            test=test+(test//2)
            return invert(y,p,test) # updated
        else:
            return test

    if p(2)<p(1):
        if p(test)<y:
            test=test//2 +(test%2)
            return invert(y,p,test) # updated
        elif p(test)>y:
            test=test+(test//2)
            return invert(y,p,test) # updated
        else:
            return test

p = polynomial([2,3,1])
t = 10**10
print(invert(3655,p,t))
def多项式(coef):
def多边形(x):
结果=0
x_n=1
对于反向(coef)中的a:
结果+=a*x\n
x_n*=x
返回结果
返回多边形
def反转(y、p、测试):#已更新
#测试=10**10#这就是问题所在
#您可以为每个递归调用重置“test”
#也就是说你会站着不动
#在达到允许的最大数量之前的任何进度
#达到递归调用。
如果p(2)>p(1):
如果p(试验)>y:
测试=测试//2+(测试%2)
返回反转(y、p、测试)#已更新

elif p(test)我现在自己编写了代码,将所有内容限制在目前为止我仅有的知识/技能范围内,并且它可以工作:

def polynomial(coef):
 def poly(x):
    result = 0
    x_n = 1  
    for a in reversed(coef):
        result += a * x_n
        x_n *= x 
    return result
 return poly

def invert(y,p):

 x=10**10

 while p(x)!=y:
    if p(x)>y:
        w=x
        x=x//2
    elif p(x)<y:
        x=(x+w)//2
    return x
def多项式(coef):
def多边形(x):
结果=0
x_n=1
对于反向(coef)中的a:
结果+=a*x\n
x_n*=x
返回结果
返回多边形
def反转(y,p):
x=10**10
而p(x)=y:
如果p(x)>y:
w=x
x=x//2

elif p(x)我现在自己编写了代码,将所有内容都限制在我目前仅有的知识/技能范围内,并且它可以工作:

def polynomial(coef):
 def poly(x):
    result = 0
    x_n = 1  
    for a in reversed(coef):
        result += a * x_n
        x_n *= x 
    return result
 return poly

def invert(y,p):

 x=10**10

 while p(x)!=y:
    if p(x)>y:
        w=x
        x=x//2
    elif p(x)<y:
        x=(x+w)//2
    return x
def多项式(coef):
def多边形(x):
结果=0
x_n=1
对于反向(coef)中的a:
结果+=a*x\n
x_n*=x
返回结果
返回多边形
def反转(y,p):
x=10**10
而p(x)=y:
如果p(x)>y:
w=x
x=x//2

elif p(x)请发布完整的回溯-但我怀疑递归太深。标识不正确。您没有将
test
设置为
10**10
作为第一个测试吗