Jan-25-2017, 06:57 PM
Hello,
I am stuck with printing the wrong result and I have not found out how to fix it. I am asked to write a program that when you input something like:
('x', '=', ('a', '-', 'c'))Â
However, I have to determine whether x is in the equation, say q, or not. If it is, I have to find out whether it is on the left or right of the operator '='. Here is my code:
I am stuck with printing the wrong result and I have not found out how to fix it. I am asked to write a program that when you input something like:
print(solve('x', (('a', '-', 'x'), '=', 'c')))it will solve for x:
('x', '=', ('a', '-', 'c'))Â
However, I have to determine whether x is in the equation, say q, or not. If it is, I have to find out whether it is on the left or right of the operator '='. Here is my code:
import types def left(e):   return e[0] def op(e):   return e[1] def right(e):   return e[2] def isInside(v,e):   if type(e) == tuple:     return isInside(v,left(e)) or isInside(v,right(e))   else:     if v == e:       return True     else:       return False def solve(v,q):   if isInside(v,left(q)):     return solving(v,q)   elif isInside(v,right(q)):     w = (right(q),'=', left(q))     return solving(v,w)   else:     return None def solving(v,q):   if type(q) == tuple:     if op(q)=='+':       return solvingAdd(v,q)     elif op(q)=='-':       return solvingSubtract(v,q)     elif op(q)=='*':       return solvingMultiply(v,q)     elif op(q)=='/':       return solvingDivide(v,q)   else:     return None def solvingAdd(v,q):   if v == left(left(q)):     return (right(q),'-',right(left(q)))   elif v == right(left(q)):     return (right(q),'-',left(left(q))) def solvingSubtract(v,q):   if v == left(left(q)):     return (right(q),'+',right(left(q)))   elif v == right(left(q)):     return (left(left(q)),'-',right(q)) def solvingMultiply(v,q):   if v == left(q):     return (right(q),'/',left(left(q)))   elif v == right(left(q)):     return (right(q),'/',right(left(q))) def solvingDivide(v,q):   if v == left(q):     return (right(q),'*',right(left(q)))   elif v == right(left(q)):     return (left(left(q)),'/',right(q))When I run it, it keeps giving me None for these test cases:
print(solve('x', (('a', '+', 'x'), '=', 'c'))) # Â ('x', '=', ('c', '-', 'a')) Â print(solve('x', (('x', '+', 'b'), '=', 'c'))) # Â ('x', '=', ('c', '-', 'b')) Â print(solve('x', (('a', '-', 'x'), '=', 'c'))) # Â ('x', '=', ('a', '-', 'c')) Â print(solve('x', (('x', '-', 'b'), '=', 'c'))) # Â ('x', '=', ('c', '+', 'b')) Â print(solve('x', (('a', '*', 'x'), '=', 'c'))) # Â ('x', '=', ('c', '/', 'a')) Â print(solve('x', (('x', '*', 'b'), '=', 'c'))) # Â ('x', '=', ('c', '/', 'b')) Â print(solve('x', (('a', '/', 'x'), '=', 'c'))) # Â ('x', '=', ('a', '/', 'c')) Â print(solve('x', (('x', '/', 'b'), '=', 'c'))) # Â ('x', '=', ('c', '*', 'b')) Â print(solve('y', ('y', '=', (('m', '*', 'x'), '+', 'b')))) # ('y', '=', (('m', '*', 'x'), '+', 'b')) Â print(solve('x', ('y', '=', (('m', '*', 'x'), '+', 'b')))) # ('x', '=', (('y', '-', 'b'), '/', 'm')) Â print(solve('a', (('b', '+', 'c'), '=', ('d', '*', (('a', '/', 'e'), '-', 'f'))))) # ('a', '=', (((('b', '+', 'c'), '/', 'd'), '+', 'f'), '*', 'e')) ÂI took C++ but Python seems pretty odd to me at this point. Thank you!