qazwsxedc
qazwsxedc

Reputation: 3

Similar methods return different results

I have 2 methods that are both supposed to return (20, 15, 3, 0, 7, -50), but the second method returns (20, 15, 3, 0, 7, -200). It probably has to do with the order of variables, but i have tried changing it and they still don't return the same answer.

Code:

def method1():
    a = 10
    b = 3
    c = 2
    d = 5
    e = 5
    f = -30
    f, b, c, d, e, a = (2*a+f)*e, b*e, c+1, (a*(7*c+2)+f*e)//(b*e), e+2, a*c
    return (a,b,c,d,e,f)
def method2():
    a = 10
    b = 3
    c = 2
    d = 5
    e = 5
    f = -30
    g,h,i,j,k,l = a,b,c,d,e,f
    d = (g*(7*i+2)+l*k)//(h*k)
    f = 2*(g+l)*k
    a = g*i
    b = h*k
    c = i+1
    e = k + 2
    return (a,b,c,d,e,f)

Upvotes: 0

Views: 126

Answers (1)

Lucas Wieloch
Lucas Wieloch

Reputation: 818

I see the confusion. The problem is the parenthesis in method2 when calculating f variable:

f = 2*(g+l)*k

which, according to your code logic is mathematically equal to:

f = 2*(a+f)*e

This makes a+f then multiplies by 2 then k. On method1 you make:

f = (2*a+f)*e

Which makes 2*a then sums f then multiplies the result by e. The mathematical operations are similar on both methods but they are different!

Upvotes: 1

Related Questions