calculate the product sum from a special array reference https dev tosfrasicaalgorithmsproductsumfromanarraydc6 python doctests can be run with the following command python m doctest v productsum py calculate the product sum of a special array which can contain integers or nested arrays the product sum is obtained by adding all elements and multiplying by their respective depths for example in the array x y the product sum is x y in the array x y z the product sum is x 2 y z in the array x y z the product sum is x 2 y 3z example input 5 2 7 1 3 6 13 8 4 output 12 recursively calculates the product sum of an array the product sum of an array is defined as the sum of its elements multiplied by their respective depths if an element is a list its product sum is calculated recursively by multiplying the sum of its elements with its depth plus one args arr the array of integers and nested lists depth the current depth level returns int the product sum of the array examples productsum1 2 3 1 6 productsum1 2 3 4 2 8 productsum1 2 3 1 6 productsum1 2 3 0 0 productsum1 2 3 7 42 productsum1 2 3 7 42 productsum1 2 3 7 42 productsum1 1 1 0 productsum1 2 1 1 productsum3 5 1 0 5 1 1 5 calculates the product sum of an array args array listunionint list the array of integers and nested lists returns int the product sum of the array examples productsumarray1 2 3 6 productsumarray1 2 3 11 productsumarray1 2 3 4 47 productsumarray0 0 productsumarray3 5 1 0 5 1 5 productsumarray1 2 1 recursively calculates the product sum of an array the product sum of an array is defined as the sum of its elements multiplied by their respective depths if an element is a list its product sum is calculated recursively by multiplying the sum of its elements with its depth plus one args arr the array of integers and nested lists depth the current depth level returns int the product sum of the array examples product_sum 1 2 3 1 6 product_sum 1 2 3 4 2 8 product_sum 1 2 3 1 6 product_sum 1 2 3 0 0 product_sum 1 2 3 7 42 product_sum 1 2 3 7 42 product_sum 1 2 3 7 42 product_sum 1 1 1 0 product_sum 1 2 1 1 product_sum 3 5 1 0 5 1 1 5 calculates the product sum of an array args array list union int list the array of integers and nested lists returns int the product sum of the array examples product_sum_array 1 2 3 6 product_sum_array 1 2 3 11 product_sum_array 1 2 3 4 47 product_sum_array 0 0 product_sum_array 3 5 1 0 5 1 5 product_sum_array 1 2 1
def product_sum(arr: list[int | list], depth: int) -> int:
    
    total_sum = 0
    for ele in arr:
        total_sum += product_sum(ele, depth + 1) if isinstance(ele, list) else ele
    return total_sum * depth


def product_sum_array(array: list[int | list]) -> int:
    
    return product_sum(array, 1)


if __name__ == "__main__":
    import doctest

    doctest.testmod()