asqapro
asqapro

Reputation: 175

Where am I going wrong with my algebra solver?

Example equation: 2x * 3x + 4x * 5x * 6x

for k in equ:
        if k == '*':
            if equ[equ.index(k)-1] == 'x':
                extraEqu1.append('1x')
                del(equ[equ.index(k)-1])
            else:
                x = equ[equ.index(k)-1]
                if 'x' in x:
                    extraEqu1.append(equ[equ.index(k)-1]) #extraEqu1.append(x)
                    del(equ[equ.index(k)-1])              #del(x)
            if equ[equ.index(k)+1] == 'x':
                extraEqu1.append('1x')
                del(equ[equ.index(k)+1])
            else:
                x = equ[equ.index(k)+1]
                if 'x' in x:
                    extraEqu1.append(equ[equ.index(k)+1]) #extraEqu1.append(x)
                    del(equ[equ.index(k)+1])              #del(x)
            del(equ[equ.index(k)])

Checks to see if any x variables are being multiplied, takes them out and puts them in a different list. I've been playing around with variations of the above code, and it always either ignores the last item or includes operators. I have no doubt there's a lot wrong with this code that I'm not seeing (but should), please point it out. Any help is appreciated.

Sorry I wasn't as clear on output. I want equ to lose any x variables being multiplied (ex: 2x * 3x both removed), but leave any that aren't being multiplied (ex: if 4x had a + on both sides of it). I want extraEqu1 to have all the x variables that equ would lose.

Input is the example equation, or something similar

I used Jeff's comment and changed the extraEqu1.append and del() in both else statements (but not the del() at the bottom) to take the x variable instead of it's value and now it works perfect. Any reason why? Commented code shows what I changed

*Note: Please don't say "Use SymPy" or any of those, I'm building my own version as a project

Upvotes: 2

Views: 302

Answers (2)

Amr
Amr

Reputation: 735

The problem is that you're looping on a list and you alter that list in the middle of the loop, so the loop ends up skipping some elements because they take a position already passed. look at this example:

>>> l = ['a','b','c','d']
>>> for e in l:
...    print e,
...    del(l[l.index(e)])
... 
a c

A possible solution is to loop on a copy of your list:

for k in equ[:]:

Also please note that del(x) in your code won't do anything, for example:

>>> l = ['a', 'b', 'c', 'd']
>>> a = l[0]
>>> del(a)
>>> l
['a', 'b', 'c', 'd']
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> 

del just removes a variable, not the object pointed to by that variable (unless there are no other references to that object, then the object too will be deleted)

also make sure to read the stuff in Corey Ogburn's comment.

EDIT: edited my previous answer because I had wrong assupmtions about the code.

Upvotes: 1

Denis
Denis

Reputation: 7343

I dont understand your task clear but if you need disassemble math expression better to use regexps python re This is a example for find all x's

import re

txt='2x * 3x + 4x * 5x * 6x'

re1='(\\d+)'    # Integer Number 1
re2='(x)'   # Any Single Character 1

rg = re.compile(re1+re2,re.IGNORECASE|re.DOTALL)
m = rg.search(txt)
if m:
    int1=m.group(1)
    c1=m.group(2)
print "("+int1+")"+"("+c1+")"+"\n"

And if you not famaliar with regexps you can try regexps generators like txt2re.com

Upvotes: 1

Related Questions