python - 不理解单链表实现的“超出递归深度”错误
问题描述
我在 linux 中为我的班级运行此代码时遇到了一些麻烦。本周我们在单链表上,我的老师给我的任务是使用节点来表示多项式并按降序列出它们,我在Node
课堂上不断遇到最大递归深度超过错误。
这是代码Node
:
#!/usr/bin/python
import sys
sys.setrecursionlimit(4500)
"""A model containing Node class"""
class Node(object):
"""A single node in a data structure"""
def __init__(self, coefficient, exponent):
self.coefficient=coefficient
self.exponent=exponent
@property
def coefficient(self):
return self.coefficient
@coefficient.setter
def coefficient(self, c):
self.coefficient=c
@coefficient.deleter
def coefficient(self):
del self.coefficient
@property
def exponent(self):
return self.exponent
@exponent.setter
def exponent(self, e):
self.exponent=e
@exponent.deleter
def exponent(self):
del self.exponent
@property
def next(self):
return self.next
@next.setter
def next(self, n):
self.next=n
@next.deleter
def next(self):
del self.next
def __eq__(self, other):
if self.exponent==other.exponent:
return True
else:
return False
def __It__(self, other):
if self.exponent<other.exponent:
return True
else:
return False
def __str(self):
if self.coefficient>=0:
sign="+"
else:
sign=""
return sign +str(self.coefficient) + "X^" +str(self.exponent)
这是我的List
课程的代码:
#!/usr/bin/python
from NodeModule import Node
class List(Node):
"""Linked list with pre-defined Node class"""
def __init__(self):
self.head=None
self.count=0
def isEmpty(self):
return self.count==0
def getSize(self):
return self.count
def insert(self, index, o, p):
if index<0 or index > self.count:
return False
n=Node(o, p)
if index==0:
n.next=self.head
self.head=n
self.count+=1
return True
walker=self.head
for i in range(index-1):
walker=walker.next
n.next=walker.next
walker.next=n
self.count+=1
return True
def delete(self, index):
if index < 0 or index > self.count:
return False
if index==0:
self.head=self.head.next
self.count-=1
return True
walker=self.head
for i in range(index-1):
walker=walker.next
walker.next=walker.next.next
self.count-=1
return True
def sort(self):
temp1=self.head.exponent
walker=self.head
j=0
while j < self.count:
for i in self.getsize():
walker=walker.next
temp2=walker.next.exponent
if walker.next.exponent > temp1:
insert(0, walker.next.coefficient, walker.next.exponent)
delete(walker.next)
while walker.next is not None:
if walker.next.exponent < walker.next.next.exponent:
insert(self.getsize(), walker.next.next.coefficient, walker.next.next.exponent)
delete(walker.next)
j+=1
def str(self):
if self.isEmpty():
return "\nEnd of Polynomial"
walker=self.head
output=[]
while walker is not None:
output.append(str(walker))
walker=walker.next
return " + " .join(output)
这是我用来测试我的代码的内容:
#!/usr/bin/python
from NodeModule import Node
from ListModule import List
def readPoly(message):
l=List()
n=input(message)
for i in range(n):
c=input("Enter the coefficient of term %d" % i)
e=input("Enter the exponent of term %d" % i)
l.insert(0, Node(c,e))
return l
def main():
l=readPoly("Enter the number of terms of the polynomial: ")
print l
l.sort()
print l
l.delete(0)
print (l)
if __name__=='__main__':
main()
口译员告诉我,错误就self.coefficient=c
在我的Node
课堂上。我该如何解决这个问题?
解决方案
您不应该有一个名为的变量coefficient
和一个同名的属性。
查看您对 setter 的定义:
@coefficient.setter
def coefficient(self, c):
self.coefficient=c
调用时self.coefficient = c
,将再次递归调用相同的设置器。
要解决它,您可以使用下划线重命名变量(并更改代码的所有其他部分):
@coefficient.setter
def coefficient(self, c):
self._coefficient = c
在进一步阅读中,您似乎可以完全省略设置器,因为它们在您的代码中没有任何用途。您的课程的简化版本Node
(具有相同的功能)可能是:
class Node:
def __init__(self, coefficient, exponent):
self.coefficient = coefficient
self.exponent = exponent
self.next = None
def __str__(self):
return '{}{}X^{}'.format(
'+' if self.coefficient >= 0 else '',
self.coefficient,
self.exponent)
推荐阅读
- html - 在 Orchard CMS 的 EditorTemplate 中渲染 PartialView
- matrix - Create Matrix from tdbc::odbc $resultset allrows -as lists
- javascript - React how to highlight multiple components by using 1 ref
- c# - dotnetzip 创建空的损坏文件
- jestjs - Jest 的 --findRelatedTests 没有注意到 JSON 文件
- webhooks - Microsoft Teams,传入 Webhook 连接器,配置保存 403 错误
- python - 如果键相同,则相交两个字典python以获取两个字典的共同元素的有效方法
- angular-material - 输入在 Angular Material 扩展面板中不起作用。无法添加空间
- java - on Activity 创建恢复微调器对象
- java - 运行 EMR 时出现“错误:无法找到或加载主类”?