python 3 prefix to infix without too many parethesis
DL Neil
PythonList at DancesWithMice.info
Mon Dec 9 15:30:59 EST 2019
On 10/12/19 8:40 AM, Terry Reedy wrote:
> On 12/9/2019 6:21 AM, jezkator at gmail.com wrote:
>> Hi, I have got a problem.
>
> Is this homework?
Same question - that way we know that our task is to help you learn to
code in Python, cf a problem with Python itself...
Similarly, you may like to know that there is a separate Python-Tutor
Discussion List.
>> I wrote a code for prefix to infix. It works, but I need improve it
>> so on input there will be only necessary parentheses.
The example code processes, like a calculator (um, yes, well...). What
do you mean by "prefix to infix"?
> Define 'necessary'; give multiple input/output examples.
> Put them in a test function.
+1
>> Here is the code:
Question: with abbreviated variableNMs, eg "p", "a", even "op"; how
easily will you comprehend the code in six month's time? How easy do you
think it is for someone else to read the code now? NB the print
functions are no help because the labels are equally-abbreviated, plus
my aged-eyes start to strain when following lists of %s-es and regexps -
mea culpa!
>> import re
>> a = input()
>>
>> class Calculator:
>>
>> def __init__ (self):
>> self.stack = []
>>
>> def push (self, p):
>> if p in ['+', '-', '*', '/', "**"]:
>> op1 = self.stack.pop ()
>> op2 = self.stack.pop ()
>> self.stack.append ('(%s%s%s)' % (op1, p, op2))
>>
>> print("op1 =", op1)
>> print("op2 =", op2)
>> print("p=", p)
>> print(self.stack)
>>
>> else:
>> self.stack.append (p)
>>
>>
>> def convert (self, l):
>> l.reverse ()
>> for e in l:
>> self.push (e)
>> return self.stack.pop ()
>>
>> c = Calculator ()
>>
>> print (c.convert (re.findall(r'\d+|\*\*|[-+*/]', a)))
>>
>>
>> input is like /-*+*++**85 27 39 87 65 65 37 63 91
ALWAYS, ALWAYS, ALWAYS, document a RegExp with a comment!!!
After 'translating' the abbreviations according to my recollection of
this form of a frequently-used ComSc course problem, I was then
surprised by the format of the input - using an infix calculator we
enter a value, then an operator, and then another value, etc; and using
a postfix calculator one usually inputs a couple of values and then an
operator, another value, and then an operator, and so on - rather than
receiving it all at once. Is this 'all at once' a feature of an infix
calculator or am I missing/forgetting something?
For which I am obviously not the only one with a confused expression on
my face...
> Since '*' and '**' are both allowed operators (see 'p in' above), spaces
> are needed between operators to disambiguate. In any case, what output
> does your code produce now, and what do you want it to produce.
I hate RegExps because it is sooooo easy to write a wrong 'un. However,
because the "\*\*" selection precedes the "[-+*/]", it appears (rheumy
eyes allowing) to enact arithmetic priority correctly. However, as
mentioned before, most of us will have created a serial and algorithmic
approach to this problem, back-when; so it does seem a little strange.
OK, let's address your actual question: the parentheses!
How do you plan to make them work? As elsewhere: please provide example
input and expected output, so that we can be sure of the code's
objective. Multiple examples can only help, especially if there are
exceptions and "edge-cases". Also, if your progress in Python is ready
for it, we can talk PyTest (or equivalent), plus such a plan enables you
to practice Test-Driven Development...
These problems stretch one's mental acuities. It would seem that I am
not the only one looking forward to hearing back from you...
--
Regards =dn
More information about the Python-list
mailing list