Assignment 3 - Sorting and Divide & Conquer Practice

Thank u so much. I made the required changes and got a pass grade. It’s so nice of you.

2 Likes

I have submitted my submission I had passed every test but only the test case 9 is fail. I couldn’t understand where the problem is, here is my code:

def multiply_optimized(poly1, poly2):
if len(poly1) == 0:
return poly2
if len(poly2) == 0:
return poly1

if len(poly1) == 1:
    product = [poly1[0] * poly2[i] for i in range(len(poly2))]
        

elif len(poly2) == 1:
    product = [poly2[0] * poly1[i] for i in range(len(poly1))]

else:
    n = max(len(poly1), len(poly2))

    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])

    Y = add(Y, [-x for x in add(U, Z)])
    Y = increase_exponent(Y, n//2)

    Z = increase_exponent(Z, (n//2)*2)

    _sum = add(Y, Z)
    _sum = add(U, _sum)
    product = _sum
    
    while product[-1] == 0:
        del product[-1]


return product

hi @niteesh-chowdary25 have u figured out how to solve test case #9
if yes, please help me

hi did you received the ceritificate?

hi guys, below is my code but it failed two test cases when i submitted the assignment Test #8 and Test #9. Does anyone know what the test cases are? If not can anyone tell what could be the reason for the failure of two test cases?

def multiply_optimized(poly1, poly2):
    #print('multiply_optimized', poly1, poly2)
    n = len(poly1)
    m = len(poly2)
    
    if len(poly1) == 0 or len(poly2) == 0:
        return [0]
    elif(m == 1 and poly2[m-1] == 0 or n == 1 and poly1[n-1] == 0):
        return [0]
    elif len(poly1) == 1:
        product = [poly1[0]* poly2[i] for i in range(len(poly2))]
    elif len(poly2) == 1:
        product = [poly2[0]* poly1[i] for i in range(len(poly1))]
    else:
        n = max(len(poly1), len(poly2))
        poly1, poly2 = split(poly1, poly2)
        #print('poly1',poly1)
        #print('poly2',poly2)
        U = multiply_optimized(poly1[0], poly2[0])
        #print('U',U)
        V = multiply_optimized(poly1[0], poly2[1])
        #print('V',V)
        W = multiply_optimized(poly1[1], poly2[0])
        #print('W',W)
        Z = multiply_optimized(poly1[1], poly2[1])
        #print('Z',Z)
        
        product = add(U, add(increase_exponent(add(V, W), n//2), increase_exponent(Z, 2*(n//2))))
        
    return product

Here is my question. Take a look and help me with this.

hello guys any clue what could be the reason for the failed result of this assignment with this code.
Test #1: PASSED
Test #2: PASSED
Test #3: PASSED
Test #4: PASSED
Test #5: PASSED
Test #6: PASSED
Test #7: PASSED
Test #8: FAILED
Test #9: FAILED
Test #10: PASSED
Test #11: PASSED
Test #12: PASSED
Test #13: PASSED
Test #14: PASSED
Test #15: PASSED
Test #16: PASSED
Test #17: PASSED
Test #18: PASSED
Test #19: PASSED
Test #20: PASSED
Test #21: PASSED
Test #22: PASSED
Test #23: PASSED
Test #24: PASSED

It means there are some border cases for which your solution fails.

If you have basic version of the algorithm working (and you’re quite sure of it) then you can generate new test casaes (many of them, randomly) and see which ones fail.

Also remember to check other extreme cases (which might be hard to generate, because their probability of showing up is low) like empty lists, lists with 0s, negative values, fractional values and/or any combination of them.

1 Like

In the first condition simply return []
You don’t need the second elif condition also

Thank you @vinaypratapsingh609 although I was covering all the test cases but was returning [0] for test case where input is 0 so maybe output was different. Your inputs helped. thanks

1 Like

thank you Sebastian, it worked

Test #3: FAILED
Test #4: FAILED
Test #5: FAILED
Test #6: FAILED

def multiply_optimized(poly1, poly2):
    if len(poly1) == 0 or len(poly2) == 0:
        return []
    elif len(poly1) == 1 :
        return [poly1[0] * poly2[i] for i in range(len(poly2))]
    elif len(poly2) == 1 :
        return [poly2[0] * poly1[i] for i in range(len(poly1))]
    else:
    #     print(poly1, poly2)
        n = max(len(poly1), len(poly2))

        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])
    
        return add(add(U, increase_exponent(add(Y, [-x for x in add(U, Z)]), n//2)), increase_exponent(Z, (n//2)*2))

same. i am not able to implement it because i dont get what and how i am supposed to do it.

Excuse me, but I do not understand why I did not pass the Assigment … All the tests were good and I still do not approve. HELP ME!

Well, one of the test has failed, so not all tests were good.

Hi friends, I got this comment on submitting my assignment 3, can anyone explain it!

Did you run the multiply_basic() function?
or
Are you using multiply_basic() function in multiply_optimized() as the base statement?

Hey guys I’m struggling with testing my cases. I used the built in function “evaluate_test_cases()” however it keeps saying that I’m missing a parameter. Can anyone please tell me which parameters to use.

Thanks in advance.

You need to use the function(which you are testing) and the test list as parameters.

Something like: evaluate_test_cases(func_name, tests).

So, if you are testing the multiply_basic() function, you statement would look like:
evaluate_test_cases(multiply_basic, tests).

If you want to test a particular test use evaluate_test_case(multiply_basic, test0).

I tried putting evaluate_test_cases(multiply_basic, tests) it says NameError: name 'multiply_basic' is not defined. ) :pensive: