Python 如何组织多边形和创建节点

Python 如何组织多边形和创建节点,python,python-3.x,list,function,linked-list,Python,Python 3.x,List,Function,Linked List,在这个类中,多项式包含一个元组列表,如>>[(1,2),(3,1),(2,3),(2,2)] 对于这个特定部分,我需要做的是获取一个元组列表,然后insert函数和init函数应该能够通过在Node类中创建节点,根据指数按升序组织数据 现在我的代码返回以下内容: >>> p = Poly([(1,0),(2,2),(1,1),(3,4)]) >>> print(p) 3x^4Traceback (most recent call last): Pyth

在这个类中,多项式包含一个元组列表,如>>[(1,2),(3,1),(2,3),(2,2)]

对于这个特定部分,我需要做的是获取一个元组列表,然后insert函数和init函数应该能够通过在Node类中创建节点,根据指数按升序组织数据

现在我的代码返回以下内容:

>>> p = Poly([(1,0),(2,2),(1,1),(3,4)])

>>> print(p)

3x^4Traceback (most recent call last):
  Python Shell, prompt 5, line 1
builtins.TypeError: __str__ returned non-string (type NoneType)
>>> p0 = Poly([(3,2), (1,0)])

>>> print(p0) 

>>> 1 + 3x^2

>>> p1 = Poly([(0,3),(4,5),(2,3),(3,4)])

>>> print(p1)

>>> 2x^3 + 3x^4 + 4x^5
这是一个链表,我不允许事先组织列表,也不允许将其转换为字典或任何其他形式。它应该从insert和init函数组织起来

我尝试在Poly init函数中创建一个while循环并分离两个元组数,但我不确定从节点类的何处开始,我非常困惑,感谢您的帮助

这就是输出的样子:

>>> p = Poly([(1,0),(2,2),(1,1),(3,4)])

>>> print(p)

3x^4Traceback (most recent call last):
  Python Shell, prompt 5, line 1
builtins.TypeError: __str__ returned non-string (type NoneType)
>>> p0 = Poly([(3,2), (1,0)])

>>> print(p0) 

>>> 1 + 3x^2

>>> p1 = Poly([(0,3),(4,5),(2,3),(3,4)])

>>> print(p1)

>>> 2x^3 + 3x^4 + 4x^5

为了正确地排序,我首先需要实现insert函数,该函数查看我的
self.\u power
将小于元组位置1的所有场景,以及大于或等于的实例。。之后,我可以在
\uuuu init\uuuu
函数中循环我的列表,并使用列表中的元组调用insert(tuple)对其进行排序

在my
\uuuu str\uuuu
函数中:


一开始,一个简单的空字符串就足够了,当我在字符串中循环时,我可以继续添加到空字符串中。

很难描述这个过程。插入器需要扫描列表,查找下一个节点的功率高于新节点的节点。这就是你在新条目中缝合的地方

class Node:
    def __init__(self, coef, power, _next=None):
        self.coef = coef
        self.power = power
        self.next = _next
    def __repr__(self):
        if self.power:
            return f"{self.coef}x^{self.power}"
        return str(self.coef)

class Poly:
    def __init__(self, lst):
        self.head = None
        for i in lst:
            self.insert( i )
     
    def __str__(self):
        if not self.head:
            return "<Poly (none)>"
        nxt = self.head
        parts = []
        while nxt:
            parts.append(str(nxt))
            nxt = nxt.next
        return "<Poly " + (' + '.join(parts)) + ">"
    
    def insert(self, tup):
        node = Node(*tup)
        if not self.head:
            self.head = node
            return
        if self.head.power == node.power:
            self.head.coef += node.coef
            return
        # Loop until the NEXT node's power is larger than the new node.
        nxt = self.head
        while nxt.next and node.power > nxt.next.power:
            if node.power == nxt.power:
                nxt.coef += node.coef
                return
            nxt = nxt.next
        node.next = nxt.next
        nxt.next = node

p = Poly([(1,0),(2,2),(1,1),(3,4)])
print(p)
p = Poly([(0,3),(4,5),(2,3),(3,4)])
print(p)

node类很好,但如果不使用它,则不需要将
\u next
作为参数。
insert
函数只需从开头开始,并按数字顺序将新节点插入其适当位置。因此,如果
\u next
不是空的,并且
\u power
大于新的power,请在此处插入新记录。明白,但由于传递的数据是带有元组的列表,我首先想知道如何创建节点?比如,我如何告诉python,元组中的第一个数字是系数,第二个数字是幂,然后交叉检查这些幂是否更大?我已经尝试了一个while循环,但是我不能让它在没有索引错误的情况下进行迭代和比较@timroberts您的代码按相反的顺序运行,我该如何按照它的本意进行排序?例如,较低的权力先走,较高的权力最后走。您显示的输出不是从代码接收的输出。我在第一段代码中显示的输出正是这两个代码段的输出。输出以功率的递增顺序出现,这是错误的,但这是您要求的。如果您没有看到结果,那么您就错误地复制了我的代码。@TimeRoberts抱歉,我的意思是,如果您有一个实例,其中元组具有相同的幂,那么用于组合这两个系数的If语句就永远不会发生,它只是跳过了它,例如<代码>[(1,0),(2,2),(1,1),(3,4),(3,2)]最终的结果将是
而不是
1+1x^1+5x^2+3x^4
啊,应该是这样的,但是有一个bug。你有没有试着追上去?第二个片段现在正确地组合了相等的幂。是的,我追查到了,你能解释一下虚拟节点的原因吗?
class Poly:
    def __init__(self, lst):
        self.head = Node(-1,-1)
        for i in lst:
            self.insert( i )
     
    def __str__(self):
        nxt = self.head.next
        parts = []
        while nxt:
            parts.append(str(nxt))
            nxt = nxt.next
        return "<Poly " + (' + '.join(parts)) + ">"
    

    def insert(self, tup):
        node = Node(*tup)
        # Loop until the NEXT node's power is larger than the new node.
        nxt = self.head
        while nxt.next and node.power > nxt.next.power:
            nxt = nxt.next
        if nxt.next and node.power == nxt.next.power:
            nxt.next.coef += node.coef
        else:
            node.next = nxt.next
            nxt.next = node