Python: Recursive function to find the largest number in the list

前端 未结 10 656
长情又很酷
长情又很酷 2020-12-10 21:04

I\'m trying to do a lab work from the textbook Zelle Python Programming

The question asked me to \"write and test a recursive function max() to find the

10条回答
  •  无人及你
    2020-12-10 21:17

    These solutions fail after certain list size.

    This is a better version:

    def maximum2(a, n):
        if n == 1:
            return a[0]
        x = maximum2(a[n//2:], n - n//2)
        return x if x > a[0] else a[0]
    def maximum(a):
        return maximum2(a, len(a))
    
    maximum(range(99999))
    
    
    >>> 99998
    

提交回复
热议问题