Pattern matching sums with positive and negative terms

Hello,

I am stuck trying to pattern match to sums with positive and negative terms. This is my activity so far: Changing the Subject • Activity by Amplify Classroom

Could someone help me with page 6 (titled Question 5) please? The code currently accepts 7-2m+4d for the numerator, but I haven’t been able to get it to also accept 7+4d-2m. It would be even better if it also accepted variations where 7 appears later eg 4d+7-2m etc as well.

Thank you!

Hande

1 Like

Honestly, unless you really want a particular form here, I think an evaluative check like below is sufficient. You could add some more evaluations to make it a little more robust. Trying to make your pattern check too specific can add significant complexity.

exp = this.latex
rhs = parseEquation(exp).rhs
expected = `\frac{7-2m+4d}{3}`

fnDiffCheck = simpleFunction(`\abs( ${expected} -(${rhs}) )`, `m`, `d`)
tolerance = 0.01
evalCheck = 
       fnDiffCheck.evaluateAt(3,4) < tolerance 
  and fnDiffCheck.evaluateAt(-3,2) < tolerance
  
check = evalCheck

One of the benefits of comparing evaluations is that you don’t have to code additional patterns to match. For example if each term was written as a fraction over 3, that’s a whole new pattern you have to deal with.

1 Like