【问题标题】:How to keep track the amount of even integers when looking through randomized list using recursion使用递归查看随机列表时如何跟踪偶数的数量
【发布时间】:2019-11-21 12:21:57
【问题描述】:

这里是新手。因此,在尝试使用递归函数时,我想尝试制作一个程序:

1:生成一个包含 0-20 范围内 10 个随机整数的列表

2:使用递归函数遍历列表并找出列表中的哪些元素是偶数

3:打印出上述偶数个数

我遇到的问题是如何打印结果。我似乎无法弄清楚在调用它时我想在函数中放入什么值( F(?) )

我尝试集成一个计数器来跟踪程序找到偶数的次数,但它总是导致错误,即无论我多么努力地将变量设为全局变量都没有定义。

我该怎么办?我完全错了吗?

import random
numL = []
for i in range(10):
    x = random.randint(0,20)
    numL.append(x)

print(numL)

def F(x):
    if numL[x] % 2 == 0:
        return numL[x]
    else:
        return F(x+1)

print(F( ??? ))

在这个论坛上提出的第一个问题,希望我没问题,感谢任何帮助!

【问题讨论】:

    标签: python python-3.x recursion


    【解决方案1】:

    假设您要返回偶数列表,那么您有 4 种情况需要考虑

    1. 这是列表中的最后一个数字,即使如此也返回此数字
    2. 这是列表中的最后一个数字,其奇数不会返回此数字
    3. 有更多的数字要检查,这个数字是偶数所以返回 this 加上函数结果
    4. 有更多的数字要检查,这个数字是奇数返回 只有函数结果而不是这个数字

    所以我们可以将其编码为

    import random
    
    def get_even_nums(nums):
        num = nums[0]
        #This is our terminating case we definitivly return a value here
        if len(nums) == 1:
            return [num] if num % 2 == 0 else []
        else:
            #If we got here we will be recursivly calling the function
            #If its even number return that number plus the result of the function
            #it its not even then just return the reult of the function and not this num
            if num % 2 == 0:
                return [num] + get_even_nums(nums[1:])
            else:
                return get_even_nums(nums[1:])
    
    
    numL = [random.randint(0, 20) for _ in range(10)]
    print(numL)
    print(get_even_nums(numL))
    

    输出

    [3, 6, 5, 10, 20, 18, 5, 0, 3, 9]
    [6, 10, 20, 18, 0]
    

    【讨论】:

      【解决方案2】:

      所以我采用了你的功能并稍微改变了它(使用稍微不同的方法)。不需要全局列表,但如果您愿意,您也可以这样做。您遇到的问题是缺少 base case 或者不正确。

      如果您使用参数0(基本上是生成的数组的第一个元素)运行原始函数,则该函数将一直运行,直到达到一个偶数。那时它将退出递归,因为基本情况基本上会在您达到偶数时停止递归调用。

      现在,要解决此问题,您必须以不同的方式解决问题。我会将您生成的数组作为函数的输入参数,然后问自己“什么是好的基本案例?”一旦到达输入列表的末尾,可能会停止递归调用。

      if len(numL) == 0:
          return ...
      

      此外,我们需要一种方法来返回我们在搜索列表中找到的偶数。出于这个原因,我将引入一个新的acc 列表,我们将在其中附加我们找到的偶数。因此函数输入参数将是

      def F(numL, acc):
          ...
      

      现在,在递归调用中,我们应该检查当前元素是否为偶数。如果是,那太好了,我们将它添加到acc 列表并继续递归调用。如果不是,我们不会向acc 添加任何内容,而是继续递归。

      if numL[0] % 2 == 0:
          acc.append(numL[0])
          return F(numL[1:], acc)
      

      综合起来,我们得到:

      def F(numL, acc):
          if len(numL) == 0:
              return  acc
          else:
              if numL[0] % 2 == 0:
                  acc.append(numL[0])
              return F(numL[1:], acc)
      

      其中numL 表示您生成的列表,acc 表示我们将在遍历列表后返回的结果列表。

      【讨论】:

        【解决方案3】:

        这是你的功能(据我了解,你想要这个):

        import random
        
        def F(i):
            r = random.randint(0,20)
            if r % 2 == 0:
                print(r)
            i += 1
            if i != 10:
                F(i)
        
        F(0)
        

        【讨论】:

          猜你喜欢
          • 2022-01-24
          • 2020-11-03
          • 2023-01-25
          • 2017-03-25
          • 2018-05-23
          • 1970-01-01
          • 2020-04-21
          • 1970-01-01
          • 2021-12-20
          相关资源
          最近更新 更多