Python recursive function doesn't return

前端 未结 3 863
情书的邮戳
情书的邮戳 2021-01-23 04:58

I\'m trying to sharpen my noob Python skills by trying a problem my son has in his college CS class. The goal is to create a function that uses recursion to process a list. The

3条回答
  •  自闭症患者
    2021-01-23 05:26

    The problem is that you don't actually do a recursion (every call to the same function returns the result) but since lists are mutable you don't need to:

    def ProcessList(ListIn):
        RecursiveProcess(ListIn, len(ListIn)-2) #this will change the list in place!
        return ListIn
    

    that's all to get it working like expected. Because each element is updated in the "recursion" so there is no need to pass the pointer to the list around. And also the result is what is expected:

    # [59, 54, 44, 33, 27, 20, 19, 17, 13, 7]
    

提交回复
热议问题