All pastes #228090 Raw Edit

Mine

public text v1 · immutable
#228090 ·published 2006-10-29 21:08 UTC
rendered paste body

#+1+a = 2

OPS = ["+", "-"]


def invertOP(sign):
    if sign == "+":
        return "-"
    elif sign == "-":
        return "+"

def solve(equa):
    equa = equa.split("=")
    if len(equa) != 2:
        print "Please enter a valid equation"
        return 0
    
    #"1 + a" and " 2"
    LS = equa[0].split() #"1","+","a"
    RS = equa[1].split() #"2"

    if LS[0] not in OPS:
        LS.insert(0, "+")


    if RS[0] not in OPS:
        RS.insert(0, "+")

    newLS = []
    newRS = []
    i = 0
    while i < len(LS) :
        if LS[i] in OPS:
            try:
                int(LS[i+1])
                newLS.append(LS[i])
                newLS.append(LS[i+1])

            except:
                RS.append(invertOP(LS[i]))
                RS.append(LS[i+1])
        i += 1

    i = 0


    while i < len(RS):
        if RS[i] in OPS:
            try:
                int(RS[i+1])
                newLS.append(invertOP(RS[i]))
                newLS.append(RS[i+1])

            except:
                newRS.append(RS[i])
                newRS.append(RS[i+1])

        i += 1

    LSeq = ""
    RSeq = ""
    for i in newLS:
        LSeq += i + " "

    for i in newRS:
        RSeq += i
        
    exec("LSeq = " + LSeq)

    print str(LSeq) + "=" + RSeq
    
  

while 1:
    inp = raw_input("Please enter equation to solve. To quit type q then enter\n")
    if inp == "q":
        break
    else:
        solve(inp)