I wanted to create a program in Reverse Polish Notation in Python (determining if a string can be converted to a number)

Introduction

This is the reason I wrote this article, but at the university test

"Create a program that calculates in Reverse Polish Notation"

For the problem. I took a lot of time in one part, so I made it for my own memo.

RPN.py


def isnum():
  if int(hoge) == True:
    return 1

  else:
    return 0

def cal(operand,a,b):
  if operand == "+":
    return int(a)+int(b)

  elif operand == "-":
    return int(a)-int(b)

  elif operand == "*":
    return int(a)*int(b)

por=[]
por = list(input().split())

stk=[]

for i in por:
  if isnum(i) == int:
  #It was a hassle here
    stk.append(int(i))

  else:
    b=stk.pop()
    a=stk.pop()
    stk.append(cal(i,a,b))

print(stk[0])

Input example.txt


1 2 * 3 +

1 2 3 4 - * 5 - +

During the test, I couldn't think of a method well, so I couldn't look it up. I was stuck in the comment above.

What couldn't be done

In Reverse Polish Notation, operands and numbers are included. In other words, it is necessary to determine whether it is a character or an operand. In the ʻisnum` function above, it is necessary to determine whether the incoming character is an int type number or a character type operand.

I couldn't implement the part of whether a string can be cast to a number here, and I couldn't program it after all.

Solution

It seems that everything can be solved by using ʻisdigit ()`. If you use isdigit (), it will return whether it can be converted to an int type.

isdigittest.py


word=["1","hato","1.0",""]

for w in word:
    print(w + " -> ",end="")
    print(w.isdigit())

output

out.txt


1 -> True
hato -> False
1.0 -> True
 -> False

It will determine if you can cast a string to an integer type like this. You can do anything with this.

If you actually use this and rewrite the above code, it will look like this.

RPN2.py


def cal(operand,a,b):
  if operand == "+":
    return int(a)+int(b)

  elif operand == "-":
    return int(a)-int(b)

  elif operand == "*":
    return int(a)*int(b)

por=[]

por = list(input().split())

stk=[]

for i in por:
  if i.isdigit() == True:
    stk.append(int(i))

  else:
    b=stk.pop()
    a=stk.pop()
    stk.append(cal(i,a,b))

print(stk[0])

change point

isnum.py


def isnum():
  if int(hoge) == True:
    return 1

  else:
    return 0

I don't need this part in the first place. If you use isdigit (), you don't need this function. After all, I became a one-liner. When I ran the above program, I was able to get the correct answer.

Answer.txt


5

-6

Recommended Posts

I wanted to create a program in Reverse Polish Notation in Python (determining if a string can be converted to a number)
Python tricks: a combination of enumerate () and zip (), checking if a string can be converted to a number, sorting the string as a number
Have python check if the string can be converted / converted to int
I want to create a window in Python
I tried to create a class that can easily serialize Json in Python
Check if the string is a number in python
I want to embed a variable in a Python string
I made a prime number generation program in Python
I made a prime number generation program in Python 2
[Python] A program to find the number of apples and oranges that can be harvested
Create a random string in Python
Check if you can connect to a TCP port in Python
I made a payroll program in Python!
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
[Python] A program that calculates the number of socks to be paired
Mayungo's Python Learning Episode 6: I tried to convert a character string to a number
I want to convert a table converted to PDF in Python back to CSV
I want to color a part of an Excel string in Python
I wanted to solve ABC159 in Python
I made a program to check the size of a file in Python
What is the fastest way to create a reverse dictionary in python?
I wanted to quickly create a mail server that can be used freely with postfix + dovecot on EC2
[Beginner] What happens if I write a program that runs in php in Python?
I made a familiar function that can be used in statistics with Python
Python program is slow! I want to speed up! In such a case ...
I tried to implement what seems to be a Windows snipping tool in Python
How to embed a variable in a python string
How to create a JSON file in Python
Create a datetime object from a string in Python (Python 3.3)
I made a Caesar cryptographic program in Python.
I tried to easily create a high-precision 3D image with one photo [1]. (Depth can now be edited in PNG.)
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
A mechanism to call a Ruby method from Python that can be done in 200 lines
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to easily implement a timeout in python
[Python] How to expand variables in a character string
Create a plugin to run Python Doctest in Vim (2)
I tried to implement a pseudo pachislot in Python
Create a plugin to run Python Doctest in Vim (1)
I want to work with a robot in python.
Only size-1 arrays can be converted to Python scalars
How to set up a simple SMTP server that can be tested locally in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
I made a program to collect images in tweets that I liked on twitter with Python
I tried to automate "one heart even if separated" using a genetic algorithm in Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
When I got a list of study sessions in Python, I found something I wanted to make
I tried to create API list.csv in Python from swagger.yaml
I tried to implement a one-dimensional cellular automaton in Python
I wrote a program quickly to study DI with Python ①
I tried "a program that removes duplicate statements in Python"
I tried "How to get a method decorated in Python"
Create a tool to check scraping rules (robots.txt) in Python
I tried to make a stopwatch using tkinter in python
Create a message corresponding to localization with python translation string
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
[Python] A program that finds the maximum number of toys that can be purchased with your money
Create a function in Python