Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python solve(v, q). Solve the equation q for the variable v, and return a new eq

ID: 3879872 • Letter: P

Question

Python

solve(v, q). Solve the equation q for the variable v, and return a new equation in which v appears alone on the left side of the equal sign. For example, if you call solve like this: solve('x', ((('m', '*', 'x'), '+', 'b'), '=', 'y')) then it will return this: ('x', '=', (('y', '-', 'b'), '/', 'm')) The function solve really just sets things up for the function solving, which does all the work. If v is inside the left side of q, then call solving with v and q. If v is inside the right side of q, then call solving with v and a new equation like q, but with its 2 left and right sides reversed. In either case, return the result of calling solving. If v is not inside q at all, then return None.

Can't figure out how to write it properly. I've got this, but it doesn't quite work.

Explanation / Answer

import types
def left(e):
    return e[0]

def op(e):
    return e[1]

def right(e):
    return e[2]

def isInside(x,e):
    if type(e) == tuple:
        return isInside(x,left(e)) or isInside(x,right(e))
    else:
        if x == e:
            return True
        else:
            return False

def solve(x,q):
    if isInside(x,left(q)):
        return solving(x,q)
    elif isInside(x,right(q)):
        w = (right(q),'=', left(q))
        return solving(x,w)
    else:
        return None
def solving(x,q):
    if type(q) == tuple:
        if op(left(q))=='+':
            return solvingAdd(x,q)
        elif op(left(q))=='-':
            return solvingSubtract(x,q)
        elif op(left(q))=='*':
            return solvingMultiply(x,q)
        elif op(left(q))=='/':
            return solvingDivide(x,q)
    else:
        return None

def solvingAdd(x,q):
    if x == left(left(q)):
        return ('x','=',right(q),'-',right(left(q)))
    elif x == right(left(q)):
        return ('x','=',right(q),'-',left(left(q)))

def solvingSubtract(x,q):
    if x == left(left(q)):
        return ('x','=',right(q),'+',right(left(q)))
    elif x == right(left(q)):
        return ('x','=',left(left(q)),'-',right(q))

def solvingMultiply(x,q):
    if x == left(q):
        return ('x','=',right(q),'/',left(left(q)))
    elif x == right(left(q)):
        return ('x','=',right(q),'/',right(left(q)))

def solvingDivide(x,q):
    if x == left(q):
        return ('x','=',right(q),'*',right(left(q)))
    elif x == right(left(q)):
        return ('x','=',left(left(q)),'/',right(q))

print(isInside('x', 'x'))                          # True   1 point
print(isInside('x', 'y'))                          # False 1 point
print(isInside('x', ('x', '+', 'y')))              # True   2 points
print(isInside('x', ('a', '+', 'b')))              # False 2 points
print(isInside('+', ('a', '+', 'b')))              # False 2 points
print(isInside('x', (('m', '*', 'x'), '+', 'b'))) # True   2 points

print(solve('x', (('a', '+', 'x'), '=', 'c')))
# ('x', '=', ('c', '-', 'a')) 2 points

print(solve('x', (('x', '+', 'b'), '=', 'c')))
# ('x', '=', ('c', '-', 'b')) 2 points

print(solve('x', (('a', '-', 'x'), '=', 'c')))
# ('x', '=', ('a', '-', 'c')) 2 points

print(solve('x', (('x', '-', 'b'), '=', 'c')))
# ('x', '=', ('c', '+', 'b')) 2 points

print(solve('x', (('a', '*', 'x'), '=', 'c')))
# ('x', '=', ('c', '/', 'a')) 2 points

print(solve('x', (('x', '*', 'b'), '=', 'c')))
# ('x', '=', ('c', '/', 'b')) 2 points

print(solve('x', (('a', '/', 'x'), '=', 'c')))
# ('x', '=', ('a', '/', 'c')) 2 points

print(solve('x', (('x', '/', 'b'), '=', 'c')))
# ('x', '=', ('c', '*', 'b')) 2 points

print(solve('y', ('y', '=', (('m', '*', 'x'), '+', 'b'))))
# ('y', '=', (('m', '*', 'x'), '+', 'b')) 2 points

print(solve('x', ('y', '=', (('m', '*', 'x'), '+', 'b'))))
# ('x', '=', (('y', '-', 'b'), '/', 'm')) 2 points

print(solve('a', (('b', '+', 'c'), '=', ('d', '*', (('a', '/', 'e'), '-', 'f')))))
# ('a', '=', (((('b', '+', 'c'), '/', 'd'), '+', 'f'), '*', 'e')) 5 points