<filename>algs01_the_largest_value/largest.py
""" 
Algorithms to find the largest value in unordered array.
"""


def largest(nums):
    """
    Requires N - 1 invocations of less-than to determine max of N > 0 elements.
    """
    my_max = nums[0]
    for idx in range(1, len(nums)):
        if my_max < nums[idx]:
            my_max = nums[idx]
    return my_max


def native_largest(nums):
    """
    Simply access built-in max() method.
    """
    return max(nums)


def alternate(nums):
    """
    In worst case, requires (1 / 2) * (N ^ 2 + 3 * N - 2) invocations of less-than.
    In best case requires N.
    """
    for v in nums:
        v_is_largest = True
        for num in nums:
            if v < num:
                v_is_largest = False
                break
        if v_is_largest:
            return v
    return None


def just_three(nums):
    """
    Returns largest in nums when given exactly three elements.
    Won't work on all problem instances.
    """
    if len(nums) != 3:
        raise ValueError('I only work on lists of size 3.')
    if nums[1] < nums[0]:
        if nums[2] < nums[1]:
            return nums[0]
        if nums[2] < nums[0]:
            return nums[0]
        return nums[2]
    if nums[1] < nums[2]:
        return nums[2]
    return nums[1]

<filename>algs01_the_largest_value/largest.py
""" 
Algorithms to find the largest value in unordered array.
"""


def largest(nums):
    """
    Requires N - 1 invocations of less-than to determine max of N > 0 elements.
    """
    my_max = nums[0]
    for idx in range(1, len(nums)):
        if my_max < nums[idx]:
            my_max = nums[idx]
    return my_max


def native_largest(nums):
    """
    Simply access built-in max() method.
    """
    return max(nums)


def alternate(nums):
    """
    In worst case, requires (1 / 2) * (N ^ 2 + 3 * N - 2) invocations of less-than.
    In best case requires N.
    """
    for v in nums:
        v_is_largest = True
        for num in nums:
            if v < num:
                v_is_largest = False
                break
        if v_is_largest:
            return v
    return None


def just_three(nums):
    """
    Returns largest in nums when given exactly three elements.
    Won't work on all problem instances.
    """
    if len(nums) != 3:
        raise ValueError('I only work on lists of size 3.')
    if nums[1] < nums[0]:
        if nums[2] < nums[1]:
            return nums[0]
        if nums[2] < nums[0]:
            return nums[0]
        return nums[2]
    if nums[1] < nums[2]:
        return nums[2]
    return nums[1]

Algorithms to find the largest value in unordered array. Requires N - 1 invocations of less-than to determine max of N > 0 elements. Simply access built-in max() method. In worst case, requires (1 / 2) * (N ^ 2 + 3 * N - 2) invocations of less-than.     In best case requires N. Returns largest in nums when given exactly three elements.     Won't work on all problem instances.