EXCEPTION An error occurred while executing the following cell

This is the code I used for the python DSA Assignment 3
def multiply_optimized(poly1, poly2):

m = len(poly1)
n = len(poly2)
o = max(m, n)

if all(p1 ==0 for p1 in poly1) or all(p2==0 for p2 in poly2):
    return [0]
elif poly1 == [] or poly2 ==[]:
    return []
elif m == 1:
    return [poly1[0]*poly2[i] for i in poly1]
elif n == 1:
    return [poly2[0]*poly1[i] for i in poly2]

if o%2 == 0:
    o = o
else:
    o = o-1

a,b = split(poly1,poly2)

Y = multiply_optimized(add(a[0],a[1]),add(b[0],b[1]))
U = multiply_optimized(a[0],b[0])
Z = multiply_optimized(a[1],b[1])

W = add(Y, subract(add(U, Z)))

return add(add(U, increase_exponent(W, o//2)),increase_exponent(Z, (o//2)*2))

This Solve all my test cases, But when I submit It comes with this error.
Test #19: EXCEPTION
An error occurred while executing the following cell:

multiply_optimized(*[], **{“poly1”: [8, 9, 2, -1, 12], “poly2”: [12, 15, 9, 7, 14]})


e[0;31m---------------------------------------------------------------------------e[0m
e[0;31mIndexErrore[0m Traceback (most recent call last)
e[0;32m/tmp/ipykernel_89/3863682234.pye[0m in e[0;36me[0;34me[0m
e[0;32m----> 1e[0;31m e[0mmultiply_optimizede[0me[0;34m(e[0me[0;34m*e[0me[0;34m[e[0me[0;34m]e[0me[0;34m,e[0m e[0;34m**e[0me[0;34m{e[0me[0;34m"poly1"e[0me[0;34m:e[0m e[0;34m[e[0me[0;36m8e[0me[0;34m,e[0m e[0;36m9e[0me[0;34m,e[0m e[0;36m2e[0me[0;34m,e[0m e[0;34m-e[0me[0;36m1e[0me[0;34m,e[0m e[0;36m12e[0me[0;34m]e[0me[0;34m,e[0m e[0;34m"poly2"e[0me[0;34m:e[0m e[0;34m[e[0me[0;36m12e[0me[0;34m,e[0m e[0;36m15e[0me[0;34m,e[0m e[0;36m9e[0me[0;34m,e[0m e[0;36m7e[0me[0;34m,e[0m e[0;36m14e[0me[0;34m]e[0me[0;34m}e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[0m
e[0;32m/tmp/ipykernel_89/1519152483.pye[0m in e[0;36mmultiply_optimizede[0;34m(poly1, poly2)e[0m
e[1;32m 21e[0m e[0mae[0me[0;34m,e[0me[0mbe[0m e[0;34m=e[0m e[0msplite[0me[0;34m(e[0me[0mpoly1e[0me[0;34m,e[0me[0mpoly2e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 22e[0m e[0;34me[0me[0m
e[0;32m—> 23e[0;31m e[0mYe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0madde[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mae[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34m,e[0me[0madde[0me[0;34m(e[0me[0mbe[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[0me[1;32m 24e[0m e[0mUe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 25e[0m e[0mZe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m

e[0;32m/tmp/ipykernel_89/1519152483.pye[0m in e[0;36mmultiply_optimizede[0;34m(poly1, poly2)e[0m
e[1;32m 21e[0m e[0mae[0me[0;34m,e[0me[0mbe[0m e[0;34m=e[0m e[0msplite[0me[0;34m(e[0me[0mpoly1e[0me[0;34m,e[0me[0mpoly2e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 22e[0m e[0;34me[0me[0m
e[0;32m—> 23e[0;31m e[0mYe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0madde[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mae[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34m,e[0me[0madde[0me[0;34m(e[0me[0mbe[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[0me[1;32m 24e[0m e[0mUe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 25e[0m e[0mZe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m

e[0;32m/tmp/ipykernel_89/1519152483.pye[0m in e[0;36mmultiply_optimizede[0;34m(poly1, poly2)e[0m
e[1;32m 21e[0m e[0mae[0me[0;34m,e[0me[0mbe[0m e[0;34m=e[0m e[0msplite[0me[0;34m(e[0me[0mpoly1e[0me[0;34m,e[0me[0mpoly2e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 22e[0m e[0;34me[0me[0m
e[0;32m—> 23e[0;31m e[0mYe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0madde[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mae[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34m,e[0me[0madde[0me[0;34m(e[0me[0mbe[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[0me[1;32m 24e[0m e[0mUe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 25e[0m e[0mZe[0m e[0;34m=e[0m e[0mmultiply_optimizede[0me[0;34m(e[0me[0mae[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m,e[0me[0mbe[0me[0;34m[e[0me[0;36m1e[0me[0;34m]e[0me[0;34m)e[0me[0;34me[0me[0;34me[0me[0m

e[0;32m/tmp/ipykernel_89/1519152483.pye[0m in e[0;36mmultiply_optimizede[0;34m(poly1, poly2)e[0m
e[1;32m 10e[0m e[0;32mreturne[0m e[0;34m[e[0me[0;34m]e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 11e[0m e[0;32melife[0m e[0mme[0m e[0;34m==e[0m e[0;36m1e[0me[0;34m:e[0me[0;34me[0me[0;34me[0me[0m
e[0;32m—> 12e[0;31m e[0;32mreturne[0m e[0;34m[e[0me[0mpoly1e[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34me[0me[0mpoly2e[0me[0;34m[e[0me[0mie[0me[0;34m]e[0m e[0;32mfore[0m e[0mie[0m e[0;32mine[0m e[0mpoly1e[0me[0;34m]e[0me[0;34me[0me[0;34me[0me[0m
e[0me[1;32m 13e[0m e[0;32melife[0m e[0mne[0m e[0;34m==e[0m e[0;36m1e[0me[0;34m:e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 14e[0m e[0;32mreturne[0m e[0;34m[e[0me[0mpoly2e[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m
e[0me[0mpoly1e[0me[0;34m[e[0me[0mie[0me[0;34m]e[0m e[0;32mfore[0m e[0mie[0m e[0;32mine[0m e[0mpoly2e[0me[0;34m]e[0me[0;34me[0me[0;34me[0me[0m

e[0;32m/tmp/ipykernel_89/1519152483.pye[0m in e[0;36me[0;34m(.0)e[0m
e[1;32m 10e[0m e[0;32mreturne[0m e[0;34m[e[0me[0;34m]e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 11e[0m e[0;32melife[0m e[0mme[0m e[0;34m==e[0m e[0;36m1e[0me[0;34m:e[0me[0;34me[0me[0;34me[0me[0m
e[0;32m—> 12e[0;31m e[0;32mreturne[0m e[0;34m[e[0me[0mpoly1e[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34me[0me[0mpoly2e[0me[0;34m[e[0me[0mie[0me[0;34m]e[0m e[0;32mfore[0m e[0mie[0m e[0;32mine[0m e[0mpoly1e[0me[0;34m]e[0me[0;34me[0me[0;34me[0me[0m
e[0me[1;32m 13e[0m e[0;32melife[0m e[0mne[0m e[0;34m==e[0m e[0;36m1e[0me[0;34m:e[0me[0;34me[0me[0;34me[0me[0m
e[1;32m 14e[0m e[0;32mreturne[0m e[0;34m[e[0me[0mpoly2e[0me[0;34m[e[0me[0;36m0e[0me[0;34m]e[0me[0;34m
e[0me[0mpoly1e[0me[0;34m[e[0me[0mie[0me[0;34m]e[0m e[0;32mfore[0m e[0mie[0m e[0;32mine[0m e[0mpoly2e[0me[0;34m]e[0me[0;34me[0me[0;34me[0me[0m

e[0;31mIndexErrore[0m: list index out of range
IndexError: list index out of range

What does this error mean?

This means that you’ve tried to access an element which lies outside of the range of the list.

BTW. your subtract function is probably incorrect. It accepts only a single value? What is it subtracting then?

But @Sebgolos My subtract function is basically a sign-changing function, And if it is really INDEX ERROR then how all my test cases are passing?
Here is my notebook link

And why they are passing inputs like this

I don’t see any error in the current version of the notebook.

You seem to use the basic version of algorithm, even though you’ve defined an optimized one, but I suppose it’s because you wanted to test if there’s something wrong with the test case.

I also don’t see anywhere the test cases you’ve used for this optimized multiplication. You got fail in test 19, while you don’t have that many.

1 Like

@Sebgolos yes now only I too notice that instead of testing the optimized one, I rapidly tested the normal one, So I don’t know the error in my optimized version. It’s a very silly mistake. Now after I will be more conscious of those small things. Thanks for spending time for me.