python 3.2 - find second smallest number in a list using recursion

后端 未结 7 1661
谎友^
谎友^ 2020-12-22 08:32

So I need to find the second smallest number within a list of integers using recursion but I cannot for the life of me devise a way to do it. I can do it with to find smalle

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-22 09:00

    def second_smallest(my_list):

    if len(my_list) == 2:
        return my_list[0] if my_list[0] > my_list[1] else my_list[1]
    
    else:
        sec_least = second_smallest(my_list[1:])
        return sec_least if sec_least < my_list[0] else my_list[1]
    

提交回复
热议问题