Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'syntax': 0.04; 'parser': 0.07; 'see.': 0.07; 'parsing': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; 'module': 0.19; 'split': 0.19; 'input': 0.22; 'otherwise,': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'mathematical': 0.24; 'math': 0.24; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'wonder': 0.29; 'message- id:@mail.gmail.com': 0.30; 'clever': 0.31; 'operators': 0.31; 'class': 0.32; 'there.': 0.32; 'could': 0.34; 'problem': 0.35; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'module.': 0.36; 'should': 0.36; 'so,': 0.37; 'starting': 0.37; 'planning': 0.38; 'pm,': 0.38; 'how': 0.40; 'solve': 0.60; 'you.': 0.62; "you'll": 0.62; 'such': 0.63; 'strategy': 0.64; 'more': 0.64; 'overall': 0.69; 'parser,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=r1iC5bGM5IlJvM56xKn7zlNVZ+bBjV2SPOEbKwsRDHE=; b=p66Bg86hU3V+fDNO46HEHDJLcnHPzMiPQrOQq7GimF513s58v3SRAKL7DQtuVrNO/W pidrqM1P4f6I+fmco/otHORHWdj+71bto4HzUNJ6vTWOxxY+CKW1/RD93pf6AFp+qyER cD2l8rE2/E8tXHJMXJev/9vaNbtLhRTvQ3Mjvp9oZLM3bWe4sebFNGY/frx9Ersd+JuQ CnBviMwPeZeJ2TcZ6TEJzO5EtGCgBeXw1lWKAOnxSNgCclc67CaiN/jPnVlR4x1LCUjx pVHkVyyB8qCpp0uF0c0jxjEAkcuMpRBFKcGq7r8fKYfUzEvVAViaGmooeopmDgu00k30 vUXg== MIME-Version: 1.0 X-Received: by 10.42.92.129 with SMTP id t1mr62807199icm.59.1412152716052; Wed, 01 Oct 2014 01:38:36 -0700 (PDT) In-Reply-To: References: <4a7f3e7a-ba4d-405e-a078-937bf861fcd8@googlegroups.com> Date: Wed, 1 Oct 2014 18:38:35 +1000 Subject: Re: how to parse standard algebraic notation From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412152724 news.xs4all.nl 2847 [2001:888:2000:d::a6]:57987 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78528 On Wed, Oct 1, 2014 at 5:01 PM, math math wrote: > What would be a good starting strategy for writing a program to take the = derivative of a polynomial expression, such as this below?: > "x**3 + x**2 + x + 1" > > I am a bit confused about my overall strategy. Should one be writing a pa= rser class to split the input on operators and then on the double asterisks= ? Are there more clever ways? Or is this something one should solve using m= athematical formulas instead of parsing the characters? > > I just wonder how a seasoned Pythonian would go about a problem like this= without using a ready-made derivative function from some module. Well, let's see. Are you planning to use exact Python syntax everywhere? For instance, will you use asterisks for multiplication ("5*x**3 + 2*x**2 - 5*x + 1")? If so, you could make use of the ast.parse() module to do some of the work for you. Otherwise, you'll have to start by writing up a grammar: exactly what is and isn't allowed? Then write a parser, and see how things go from there. ChrisA