【问题标题】:The incrementing here continues to return a 0 value这里的递增继续返回0值
【发布时间】:2021-06-24 15:04:47
【问题描述】:

我正在编写this codewars 问题的解决方案,但是我遇到了一些问题。

问题陈述:

编写一个函数persistence,它接受一个正参数num 并返回其乘法持久性,这是您必须将num 中的数字相乘直到达到一个数字的次数,例如:

persistence(39) # 返回 3,因为 39=27, 27=14, 1*4=4 和 4 只有一位数

def persistence(n, t=1, x=0):
    if len(str(n)) > 1:
        number = [int(i) for i in str(n)]
        for i in number:
            t = t * i
        if len(str(t)) > 1:
            x += 1
            return(persistence(t,x))
        else:
            return(x)
    else:
        return 0

我无法弄清楚这段代码中的错误是什么。我的直觉是,要么是参数错误,要么是 return() 值的放置方式。

本质上,将整数提取为倍数的代码是正确的,所以我只是在持久性中添加了一个额外的参数;设置 x = 0 并使其每次满足 if 条件时都会增加确切的 x 值。一旦数字被蒸馏出来,只需输出 x。然而,它继续简单地输出 0 作为最终答案。这里有什么问题?

编辑:解决方案在 cmets 中,没有意识到参数是如何传递的。正确的版本是:

return(persistence(t,1,x))

还必须设置 x = 1 才能使逻辑在代码战上运行。

【问题讨论】:

  • return(persistence(t,x)) 你知道t 将作为n 参数传递,x 将作为t 传递吗?
  • @JohnGordon 啊,我怀疑这与参数有关。我知道 t 作为 n 传递,因为代码需要这样做,但我没想到以下参数也会按顺序排列。我假设正确的版本是 persistence(t,1,x)
  • t 应该是一个局部变量而不是一个参数。而不是使用x,您可以只使用return 1 + persistence(t)

标签: python recursion


【解决方案1】:

你的代码有两个缺陷:

return(persistence(t,x))

应该是

return(persistence(t,1,x))

否则 x 的值将被分配给 t 并且 x 将被默认为 0。

那么你必须在第一次测试后直接增加 x,否则你会错过一次迭代。

另一种计算方法不是切换到字符串,而是用数字进行:

def persistence(n):
    iterations = 0;           # no iterations yet
    while n > 9:              # while n has more than 1 digit:
        product = 1              # neutrum for result product
        while n > 0:             # while there a digit to process:
            digit = n % 10          # retrieve the right most digit
            product *= digit        # do the multiplication
            n = n // 10             # cut off the processed digit
        iterations += 1          # increment iterations
        n = product              # let n be the newly calculated product
    return iterations         # return the result

【讨论】:

    【解决方案2】:

    我认为你的函数参数不像你期望的那样工作。 当你调用函数persistence(t, x)时,第一个参数n应该变成t,第二个参数x应该变成新的x。但是在你的函数中,x 会因为它们的位置而变成新的t

    拥有一堆print 语句来揭示错误非常有用。

    def persistence(n, x=1, t=1):
        print('x:', x)
        if len(str(n)) > 1:
            number = [int(i) for i in str(n)]
            for i in number:
                t = t * i
            print('t:', t)
            if len(str(t)) > 1:
                x += 1
                print('x has changed:', x)
                return persistence(t, x)
            else:
                return x
        else:
            return 0
    
    
    print(persistence(39))
    print('---------------')
    print(persistence(999))
    print('---------------')
    print(persistence(4))
    
    
    

    【讨论】:

      【解决方案3】:

      通过两个更改的所有测试用例:

      • 您没有每次都用新的t 更新您的n

      • 你的 x 每次都被设置为 0。那应该在开头设置为1(默认值)

        def persistence(n, t=1, x=1):
          if len(str(n)) > 1:
              number = [int(i) for i in str(n)]
              for i in number:
                  t = t * i
              if len(str(t)) > 1:
                  x += 1
                  return (persistence(n=t, x=x))
              else:
                  return (x)
          else:
              return 0
        

      其实不用tn这两个参数也可以写。一个n就可以了,如下图:

      def persistence(n, x=1):
          if len(str(n)) > 1:
              number = [int(i) for i in str(n)]
      
              t = 1
              for i in number:
                  t = t * i
      
              if len(str(t)) > 1:
                  return x + (persistence(n=t, x=x))
              else:
                  return (x)
          else:
              return 0
      

      【讨论】:

        猜你喜欢
        • 2012-05-26
        • 1970-01-01
        • 2018-07-15
        • 2012-01-30
        • 2012-03-14
        • 1970-01-01
        • 1970-01-01
        • 2022-11-20
        • 2021-11-28
        相关资源
        最近更新 更多