-1

This function works only when I call with:

def prodo(L):
 sum = 1
 for i in range(0, len(L)):
    sum = L[i] * sum
 return sum

these lines:

d = prodo([3, -1, 4, -1, -5])
print(d)

But the following function using L returns 375 and not -60

def prod(L):
    sum = 1
    for i in L:
        if i>=len(L):
            break
        sum = L[i] * sum
    return sum

Question: What am I doing wrong and why?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
  • 3
    I think you'll see the difference if you print out `i` in the second version – Skam Jan 02 '18 at 02:46
  • [what-exactly-are-pythons-iterator-iterable-and-iteration-protocols](https://stackoverflow.com/questions/9884132/what-exactly-are-pythons-iterator-iterable-and-iteration-protocols) might be helpful – Skam Jan 02 '18 at 02:49

4 Answers4

1

You can do it like this:

from functools import reduce

product = reduce((lambda x, y: x * y), [3, -1, 4, -1, -5])
print(product)

This outputs -60

Optionally, you can declare any list

l = [3, -1, 4, -1, -5]

and then call

product = reduce((lambda x, y: x * y), l)
dmitryro
  • 3,463
  • 2
  • 20
  • 28
1

As @Skam proposes in comments, you will know the difference if you try to print i in your second function. When you use the below syntax:

for i in L

i does not represent a numeric value that starts from 0 and goes to the length of your list. But it represents the item itself. As an example, consider the below code:

L = [1,3,4,5,6]
for i in L:
    print i

The output of this code is:

1
3
4
5
6

So in your second function when you are trying to do L[i], you are not actually getting the current iteration number and trying to access it in your list, but you are trying to access the value that has the index of the current value of i , considering the mentioned code, if I try to do L[i] the first iteration will print 3 since it has the index of 1 which is the first element.

ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51
1

In the def prod() you are using the i as value of the elements of the list.

So when you do:

def prod(L)
    for i in L: # i = 3, -1, 4, -1, -5

When you write:

for i in range(0,L): # i =0, 1, 2, 3, 4

These are the places you need to change the code.

  • Please expand on your answer to explain what is wrong and why and what it should be replaced with and why. – jwpfox Jan 02 '18 at 03:01
1

This function doesn't work because you are not iterating over the array like this:

for i in range(len(L)):
    // now `i` will have values 0,1,2,3,...,len(L)-1

instead you are iterating like this:

d = prodo([3, -1, 4, -1, -5])

for i in L:
    // now i will take values 3,-1,4,-1,-5

With this method you cannot use the if i == len(L): to determine if you are at the end of the array, because i is not a number index but rather the value at a certain index.

So the break will execute any time a number from the array is higher or equal to the length of the array.

def prod(L):
    sum = 1
    for i in L:
        if i>=len(L):
            break
        sum = L[i] * sum
    return sum
Ivan86
  • 5,695
  • 2
  • 14
  • 30