【问题标题】:Each value in the node of a chain is calculated from the previous value plus the sum of the digits of the previous value链节点中的每个值都是从前一个值加上前一个值的数字之和计算出来的
【发布时间】:2017-06-19 13:51:04
【问题描述】:

我对这类事情很陌生,我正在尝试找到一个函数,该函数需要两个整数值、起始值以及链中总共应该有多少个节点。节点链中的每个值都是根据前一个值加上前一个值的数字之和计算得出的。例如:

(409, 5)

会生成一串

409
422
430
437
451

我现在的代码:

class Node:
    def __init__(self, init_data):
        self.data = init_data
        self.next = None

    def get_data(self):
        return self.data

    def get_next(self):
        return self.next

    def set_data(self, new_data):
        self.data = new_data

    def set_next(self, new_next):
        self.next = new_next

    def __str__(self):
        return str(self.data)

def generate_chain(start, n):
    node = Node(start)
    current = start
    string_of_nodes = str(start)
    list_of_nodes = []
    print(current)
    for digit in string_of_nodes:
        list_of_nodes.append(int(digit))
    for i in range(n-1):
        node.set_next(sum (int(a) for a in list_of_nodes) + int(current))
        current += (node.get_data())
        print(current)

生成以下输出:

409
818
1227
1636
2045
2454

我想知道是否有人可以帮助我找到我的错误并指导我正确的方式。谢谢。

【问题讨论】:

    标签: python python-3.x linked-list nodes


    【解决方案1】:

    使用Node 类的替代实现如下:

    def generate_chain(start, n):
        head = node = Node(start)
        for _ in range(n):
            sum_of_digits = 0
            v = node.get_data()
            while v > 0:
                sum_of_digits += v % 10
                v //= 10
    
            node.set_next(Node(node.get_data() + sum_of_digits))
            node = node.get_next()
    
        node = head
        while node:
            print(node.get_data())
            node = node.get_next()
    

    此代码创建一个节点链,然后打印。原始代码中的问题似乎是链的构造不正确,因为 set_next 是用 int 而不是 Node 调用的。

    【讨论】:

    • 感谢您澄清我哪里出错了。还有什么线head = node = Node(start) 与去head = Node(start) 不同; node = Node(start) 分两行。我以前没见过。
    • 我也不知道while node:这行应该做什么。
    • 关于head = node = Node(start)node = Node(start)head = Node(start),区别在于headnode 将指向后者中的两个不同对象。然而,它可以写成head = Node(start)node = head。关于while v > 0,这是对数字求和的另一种方法。然而,我认为@Tristan 对数字求和的方式更优雅。关于while node,它应该一个一个地打印节点值,直到我们到达最后一个。那么node的值就是None,循环结束。
    【解决方案2】:

    我想这可以很好地实现为生成器:

    def chain(steps, value):
        for i in range(steps):
            yield value
            value+=sum([int(i) for i in str(value)])
    
    x = chain(5, 409)
    for i in x:
        print(i)
    

    如果要使用Node类,可以使用这个函数:

    def generate_chain(start, n):
        node = Node(start)
        for k in range(n):
            yield node.get_data()
            node.set_next(node.get_data() + sum(int(i) for i in str(node)))
            node.set_data(node.get_next())
            value = node.get_data()
    
    I = generate_chain(409, 5)
    for i in I:
        print(i)
    

    【讨论】:

      【解决方案3】:

      使用s.append(x)函数和算术模运算符%的短解:

      def generate_chain(num, total):
          chain = [num]  # setting the initial value
          total -= 1
          while total:
              chain.append(chain[-1] + chain[-1] // 100 + chain[-1] // 10 % 10 + chain[-1] % 10)
              total -= 1
          return chain
      
      print(generate_chain(409, 5))
      

      输出:

      [409, 422, 430, 437, 451]
      

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 2020-11-29
        • 1970-01-01
        • 2019-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多