Jan-08-2019, 01:24 PM
(Jan-08-2019, 12:48 PM)perfringo Wrote: Is the code you provided intended correctly?
It suspect that problem is that you overwriting combination value with every loop iteration and therefore you get only last result.
>>> combinations = list() >>> for i in range(3): ... combinations.append([1, 2, 3]) ... >>> combinations [[1, 2, 3], [1, 2, 3], [1, 2, 3]] >>> for row in combinations: ... print(sequence_product(row)) ... 6 6 6
I think the problem is when I tried the combinations in new code this does not work.
So what I do now is:
1 step)=> run the code until containing print(combinations), this will give me a result(see solution box above)
2 step )=> Use this code given
from functools import reduce from operator import mul def sequence_productx(combinations): """Return sequence elements product; if empty return zero.""" if not combinations: return 0 else: return reduce(mul, combinations)3 step ) =>
sequence_productx(combinations)sequence_productx(combinations) # this print out 32768
4 step ) = > problem is I don't get answer 96, 128, 192, 13824, 32768.