

def product_except_one(arr):
    if len(arr) == 0:
        return []

    prod = arr[0]
    backup = arr[0]
    for el in arr[1:]:
        if el == 0 and backup != prod:
            backup = prod = 0
            break
        if el != 0:
            backup *= el
        prod *= el

    return [prod / el if el != 0 else backup for el in arr]


def product_except_one_2(arr):
    out = []
    prod = 1
    for el in arr:
        out.append(prod)
        prod *= el

    prod = 1
    for i in xrange(len(arr) - 1, -1, -1):
        out[i] *= prod
        prod *= arr[i]

    return out


if __name__ == '__main__':
    print product_except_one_2([1, 2, 3, 4, 5])
    print product_except_one([1, 2, 3, 4, 5])
    print product_except_one_2([1, 0, 3, 0, 5])
    print product_except_one([1, 0, 3, 0, 5])
    print product_except_one_2([1, 2, 3, 0, 5])
    print product_except_one([1, 2, 3, 0, 5])