3

I'm writing a small app in python which lets users answer math questions. The problem I'm having is checking their answers.

Say there's a question like: "Factorise x^2 + 3x +2"

There are different ways to answer this; for example:

  • (x + 1)(x + 2)
  • (x + 2)(x + 1)
  • (2 + x)(x + 1)
  • etc.

Is there a library which will check if an answer is equivalent to another? Particularly one which doesn't simplify the given answer; so:

(x + 1)(x + 2) === (2 + x)(x + 1)

But

(x + 1)(x + 2) !== x^2 + 3x +2

I thought about using wolframalpha for this — would this be possible — and if so what syntax should I use?

Thanks!

bluepnume
  • 16,460
  • 8
  • 38
  • 48

5 Answers5

1

You could try using a symbolic math library like sympy.

Call the simplify logic on both your answer and the one supplied by the user. Running the logic on both addresses this issue noted in the documentation:

exact strategies this function tries can change in the future versions of SymPy

nsanders
  • 12,250
  • 2
  • 40
  • 47
  • This looks very promising -- thanks for the suggestion. There's still the problem that the user could just input the original question, and have it flagged as correct, however... – bluepnume Dec 21 '11 at 02:22
  • @bluepnume For the specific case of factoring polynomials, you could ensure that all occurrences of 'x' are to the first power only. Maybe that coupled with a regex to enforce the structure of the solution. – nsanders Dec 21 '11 at 04:10
0

Yeah Wolfram Alpha will handle this (just stick your conditions into the search and it'll return a boolean value).

You obviously don't want to do that for each one so you could turn to the Wolfram Alpha API. They have a Python library and it's free for non-commercial use, up to 2000 calls per month. If you need commercial calls or more calls, they have monthly tariffs.

There are local options (like Python) but you'll likely have more problems with syntax, formatting, making sure your users aren't passing in malicious Python code and just supporting everything. The WA-API should remove most of the problems for you but test it first.

Oli
  • 235,628
  • 64
  • 220
  • 299
  • Sounds great, but can you think of a way to prevent the user from just inputting the question as their answer, and having it marked as correct? – bluepnume Dec 21 '11 at 02:24
  • Well that's more simple. If you're factorising, you just need to look for certain things in the answer like making sure there's two groups of parenthesis, separate x groups, etc. But a user could still stick the questions into WA and ace the test. – Oli Dec 21 '11 at 10:29
0

You may try using SymPy. It may help solving your problem. http://docs.sympy.org/dev/gotchas.html#double-equals-signs

ovgolovin
  • 13,063
  • 6
  • 47
  • 78
0

You may want to look at wims. AFAIK it is not written in python, but it is open source, so you can look at the code and get some ideas.

0

I don't think you need a library for something like that. You have the user input limited to answers your program understands (strings that make sense) and then do the reverse operation to find if their answer is correct by matching the question, or take the easy way out and have radio button multiple choice. So using your example you have your program expand out the answer they give, and check it against x^2 + 3x +2.

daniel
  • 471
  • 1
  • 4
  • 13
  • There's at least 10 different permutations of the correct answer to the example in this question. do you seriously think it's a good idea to require the user/teacher to input ten different correct answers? The problem should be at least partly solvable with basic arithmetic rules. – Emil Vikström Dec 21 '11 at 00:02
  • With my method the program doesn't need to have the 10+ answers as strings to check the input against. It takes the user input (the users answer), generates the expansion and checks it against its copy of what was asked (the question the teacher entered). For the factorizing example the program would take the the input, tokenize and then do some multiplying and spit out the result in a standard form (like a string or an array of the coefficients). It is pretty easy to write code that keeps its answers in the same form. – daniel Dec 21 '11 at 00:39
  • Then I must have misunderstood your answer :-) – Emil Vikström Dec 21 '11 at 09:58