how to parse standard algebraic notation
Chris Angelico
rosuav at gmail.com
Wed Oct 1 02:20:50 CEST 2014
On Wed, Oct 1, 2014 at 6:53 AM, math math <mathematisch at gmail.com> wrote:
> I want to basically write a program that inputs a polynomial in standard algebraic notation and outputs its derivative.
>
> I know that I need to get the exponent somehow, but I am not sure how to accomplish this in python (3.3)
As Gary says, the form of input is really the critical part. How will
users type these?
One simple cheat you could do would be to prompt for each term
separately - something like this:
exponent = int(input("What is the highest exponent used? "))
terms = []
while exponent:
terms.append(input("Enter coefficient of x^%d: " % exponent))
exponent -= 1
terms.append(input("Enter constant term: "))
You could do something similar with a GUI toolkit like tkinter or
pygtk, too; provide input fields for the coefficients, and labels in
between that say "x² + " etc.
Otherwise, you're going to be primarily bound by the keyboard - your
program is useless if it takes longer to key data into it than to do
the work manually. Python 3.3 has excellent Unicode support, and
Unicode has excellent support for mathematical notations, so you
should be able to produce nice-looking output; but mathematical
notation is much easier to write on a blackboard than on a computer's
keyboard. (And even though some keyboards are black, like the one I'm
using right now, it doesn't make it any easier. Sorry.) Gary's
suggestions of the double asterisk and caret are two commonly-used
notations for exponentiation in computers, so either would make some
sense. Otherwise, you could come up with some other notation, but
mainly, it has to be typeable.
But hey! Once you have something you can type unambiguously, we can
help you parse that into something you can make use of! That part's
not too hard.
ChrisA
More information about the Python-list
mailing list