且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在数字列表中找到最小数字的递归方法

更新时间:2023-02-19 23:15:23

这是min的递归实现:

l=[5, 3, 9, 10, 8, 2, 7]
def find_min(l,current_minimum = None):
    if not l:
        return current_minimum
    candidate=l.pop()
    if current_minimum==None or candidate<current_minimum:
        return find_min(l,candidate)
    return find_min(l,current_minimum)
print find_min(l)
>>>
2     

考虑到这不应在实际程序中使用,而应视为练习.性能会比内置的min差几个数量级.

Take into account that this should not be used in real programs and should be treated as an exercise. The performance will be worse than the built-in minby several orders of magnitude.