Home>

### python reverse polish notation calculation algorithm

Please write your question in detail here.
This is a Python question.
I want to display the answer of an expression like 2 1+ written in Reverse Polish Notation

(1) At the time of calculation, two numbers (stack  and stack ) are taken out from the stack and the calculation is performed.
(2) When the calculation is completed, delete the two used numbers from the stack, and then put the answer of (1) on the stack.
③ As a result, the last answer is at the top of the stack
You can use input, print, while, if, len, str
The following error message occurred while implementing the above function. Please tell me how to make it work properly.

``````stack = shiki [n] + stack
TypeError: can only concatenate str (not "list") to str``````
Corresponding source code
``````Source code
shiki = input ()

stack = []
a = len (shiki)
n = 0
while n<= a:
if shiki [n] == "+":
register2 = int (stack )
register1 = int (stack )
kotae = register1 + register2
stack = stack [2:]
stack = str (kotae) + stack

else: else:
stack = shiki [n] + stack
kotae = stack 
print (kotae)``````
What I tried

Please describe here what you have tried for the problem.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

• Answer # 1

Well, usually to insert an element anywhere in the list`insert`Is used. If you want to add it to the beginning (position 0)`stack.insert (0, shiki [n])`is not it.

However, this time`insert`It's like a binding that can't be used, so use slices to get the same result.`stack [0: 0] = shiki [n]`is. The process of addition is the same.

Besides this,

To get the "answer of an expression like 2 1 +", you need to skip the blanks (no need for an input like "2 1+")

`n`The process to increase is not written

The boundary condition for repeating while is incorrect

Please solve the problem yourself.

• Answer # 2

It looks good, but there are some problems.

■ The part that caused the error.
The process of adding data to the top of the stack must be done between lists.
【Part 1】

``  stack = str (kotae) + stack``
``  stack = [str (kotae)] + stack``

[Part 2]

``stack = shiki [n] + stack``
``stack = [shiki [n]] + stack``

■ Input must be listed
I don't know how to enter it, but if you separate it with spaces like "1 3 4 +", divide it with spaces to make a list.

``shiki = input (). split ((''))``

■ The n increment in while is missing.
Add the following line to the end of the while loop

`` n + = 1``

■ The loop condition is incorrect.

Since n starts from 0, the judgment of while is`n<= a`not`n is.`

``` I think this will work. --- bonus It's hard to get values ​​in and out of the top of a python list. On the other hand, if you are from the back, you can use pop and append to make it stack-like. If you write only the while part, it looks like this. (adsbygoogle = window.adsbygoogle || []).push({}); python while n if shiki [n] == "+": register2 = int (stack.pop ()) register1 = int (stack.pop ()) kotae = register1 + register2 stack.append (str (kotae)) else: else: stack.append (shiki [n]) n + = 1 `` ``` * Normally, push is paired with pop in stack, but append is used in python. ```
``` ```
• ``` ```
``` ```
``` ```
``` Related articlespython 3x - about array calculation in pythonpython - i want to speed up the calculation of for statements with numpypython polygon area calculation can't assign to operatori want to automatically generate a calculation problem in pythonpython - saving the calculation result of the correlation coefficientthe calculation result of python print (1/2) becomes 0error when performing numerical calculation with pythonpython - about the reason why the notation of the sample of the decision tree visualized by graphviz changes depending on the prpython 3x - about the calculation of cross entropy in pytorchpython contour detection&centroid calculationmultiple classification score calculation errors, python, svmpython - how to convert exponential notation to decimal notationpython - about the average value calculation method of mass calculationpython - pass the calculation defined by the function for all combinations of arrayspython time calculationpython 3x - calculation method of distance between data using log-likelihoodpython - i want to calculate matrix calculation with numpy for statement with numpydot ```
``` ```
``` Trends python - you may need to restart the kernel to use updated packages errorphp - coincheck api authentication doesn't workphp - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site[php] i want to get account information using coincheck apithe emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like tojavascript - how to check if an element exists in puppeteeri want to call a child component method from a parent in vuejspython 3x - typeerror: 'method' object is not subscriptabledart - flutter: the instance member'stars' can't be accessed in an initializer errorxcode - pod install [!] no `podfile 'found in the project directory (adsbygoogle = window.adsbygoogle || []).push({}); ```
``` ```
``` (function() { if((".artleft pre").length > 0){ \$(".artleft pre").addClass('prettyprint linenums'); prettyPrint(); }; \$(".artleft p > code").each(function() { if (\$(this).text().length >= 30) { \$(this).addClass('linecode'); }else{} }); })(); ```
``` ```
``` © 2019 Programming tutorial for beginners ```