Find the second smallest number in a list using recursion

后端 未结 6 1978
故里飘歌
故里飘歌 2021-02-19 18:08

I know there has been a question asked on this topic, but none of the answers have helped me. I don\'t need help with implementing the code, I just need help sorting through the

6条回答
  •  被撕碎了的回忆
    2021-02-19 18:35

    You can use a flag to keep track whether you're in the most outer level of the calls.

    def second_smallest(numbers, top_level=True):
        # Do your stuff and call `second_smallest(numbers[1:], False)` for any recursions.
        # When it comes to returning a value from the function, use the following.
    
        if top_level:
            return result[0]   # what your function ultimately returns
        return result          # [second, first] since you're still in recursive calls
    

提交回复
热议问题