educative.io

Educative

Checking if this solution is good or bad

Hi,

I came up with this solution for challenge 4 (list of products of all elements), it works but not sure if it is good. Could you give me feedback, Thanks!

def find_product(lst):
p = 1
result = []
num_zeros = lst.count(0)

for i in range(len(lst)):
    if lst[i] != 0:
        p *= lst[i]

for j in range(len(lst)):
    if 0 in lst:
        if num_zeros > 1:
            result.append(0)
        elif lst[j] == 0:
            result.append(p)
        else:
            result.append(0)

    else:
        result.append(p / lst[j])

return result

Type your question above this line.

Course: https://www.educative.io/collection/5642554087309312/5634727314718720
Lesson: https://www.educative.io/collection/page/5642554087309312/5634727314718720/5759700863811584

Hi @Annie

You did a great job while solving this challenge. But by looking into your solution, I have some doubts that there are some unnecessary things there.

  1. In the first loop of the code, you are excluding the zero value. Why so? Even it is not a condition of the problem?
  2. In last else, why are you dividing p by lst[j] ?