mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Compare commits
8 Commits
b4521ea28f
...
51df05ce11
Author | SHA1 | Date | |
---|---|---|---|
|
51df05ce11 | ||
|
0b1627a12d | ||
|
d3f4ee8bbd | ||
|
b0c44a6856 | ||
|
b2bb0afd5e | ||
|
c11b18fed8 | ||
|
9a15148f8b | ||
|
b4f51a6ac3 |
@ -1,8 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
The Reverse Polish Nation also known as Polish postfix notation
|
Reverse Polish Nation is also known as Polish postfix notation or simply postfix
|
||||||
or simply postfix notation.
|
notation.
|
||||||
https://en.wikipedia.org/wiki/Reverse_Polish_notation
|
https://en.wikipedia.org/wiki/Reverse_Polish_notation
|
||||||
Classic examples of simple stack implementations
|
Classic examples of simple stack implementations.
|
||||||
Valid operators are +, -, *, /.
|
Valid operators are +, -, *, /.
|
||||||
Each operand may be an integer or another expression.
|
Each operand may be an integer or another expression.
|
||||||
|
|
||||||
@ -27,119 +27,109 @@ Enter a Postfix Equation (space separated) = 5 6 9 * +
|
|||||||
# Defining valid unary operator symbols
|
# Defining valid unary operator symbols
|
||||||
UNARY_OP_SYMBOLS = ("-", "+")
|
UNARY_OP_SYMBOLS = ("-", "+")
|
||||||
|
|
||||||
# Defining valid binary operator symbols
|
# operators & their respective operation
|
||||||
BINARY_OP_SYMBOLS = ("-", "+", "*", "^", "/")
|
OPERATORS = {
|
||||||
|
|
||||||
|
|
||||||
def get_number(data: str) -> int | float | str:
|
|
||||||
"""
|
|
||||||
Converts the given data to appropriate number if it is indeed a number, else returns
|
|
||||||
the data as it is with a False flag. This function also serves as a check of whether
|
|
||||||
the input is a number or not.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
data : str
|
|
||||||
The data which needs to be converted to the appropriate number
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
bool, int or float
|
|
||||||
Returns a tuple of (a, b) where 'a' is True if data is indeed a number (integer
|
|
||||||
or numeric) and 'b' is either an integer of a floating point number.
|
|
||||||
If 'a' is False, then b is 'data'
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
return int(data)
|
|
||||||
except ValueError:
|
|
||||||
try:
|
|
||||||
return float(data)
|
|
||||||
except ValueError:
|
|
||||||
if is_operator(data):
|
|
||||||
return data
|
|
||||||
msg = f"{data} is neither a number nor a valid operator"
|
|
||||||
raise ValueError(msg)
|
|
||||||
|
|
||||||
|
|
||||||
def is_operator(data: str | int | float) -> bool:
|
|
||||||
"""
|
|
||||||
Checks whether a given input is one of the valid operators or not.
|
|
||||||
Valid operators being '-', '+', '*', '^' and '/'.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
data : str
|
|
||||||
The value that needs to be checked for operator
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
bool
|
|
||||||
True if data is an operator else False.
|
|
||||||
"""
|
|
||||||
return data in BINARY_OP_SYMBOLS
|
|
||||||
|
|
||||||
|
|
||||||
def evaluate(post_fix: list[str], verbose: bool = False) -> int | float | str | None:
|
|
||||||
"""
|
|
||||||
Function that evaluates postfix expression using a stack.
|
|
||||||
>>> evaluate(["2", "1", "+", "3", "*"])
|
|
||||||
9
|
|
||||||
>>> evaluate(["4", "13", "5", "/", "+"])
|
|
||||||
6.6
|
|
||||||
>>> evaluate(["2", "-", "3", "+"])
|
|
||||||
1
|
|
||||||
>>> evaluate(["-4", "5", "*", "6", "-"])
|
|
||||||
-26
|
|
||||||
>>> evaluate([])
|
|
||||||
0
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
----------
|
|
||||||
post_fix : list
|
|
||||||
The postfix expression tokenized into operators and operands and stored as a
|
|
||||||
python list
|
|
||||||
|
|
||||||
verbose : bool
|
|
||||||
Display stack contents while evaluating the expression if verbose is True
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
int
|
|
||||||
The evaluated value
|
|
||||||
"""
|
|
||||||
x: str | int | float
|
|
||||||
stack = []
|
|
||||||
valid_expression = []
|
|
||||||
opr = {
|
|
||||||
"^": lambda p, q: p**q,
|
"^": lambda p, q: p**q,
|
||||||
"*": lambda p, q: p * q,
|
"*": lambda p, q: p * q,
|
||||||
"/": lambda p, q: p / q,
|
"/": lambda p, q: p / q,
|
||||||
"+": lambda p, q: p + q,
|
"+": lambda p, q: p + q,
|
||||||
"-": lambda p, q: p - q,
|
"-": lambda p, q: p - q,
|
||||||
} # operators & their respective operation
|
}
|
||||||
if len(post_fix) == 0:
|
|
||||||
|
|
||||||
|
def parse_token(token: str | float) -> float | str:
|
||||||
|
"""
|
||||||
|
Converts the given data to the appropriate number if it is indeed a number, else
|
||||||
|
returns the data as it is with a False flag. This function also serves as a check
|
||||||
|
of whether the input is a number or not.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
token: The data that needs to be converted to the appropriate operator or number.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
float or str
|
||||||
|
Returns a float if `token` is a number or a str if `token` is an operator
|
||||||
|
"""
|
||||||
|
if token in OPERATORS:
|
||||||
|
return token
|
||||||
|
try:
|
||||||
|
return float(token)
|
||||||
|
except ValueError:
|
||||||
|
msg = f"{token} is neither a number nor a valid operator"
|
||||||
|
raise ValueError(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def evaluate(post_fix: list[str], verbose: bool = False) -> float:
|
||||||
|
"""
|
||||||
|
Evaluate postfix expression using a stack.
|
||||||
|
>>> evaluate(["0"])
|
||||||
|
0.0
|
||||||
|
>>> evaluate(["-0"])
|
||||||
|
-0.0
|
||||||
|
>>> evaluate(["1"])
|
||||||
|
1.0
|
||||||
|
>>> evaluate(["-1"])
|
||||||
|
-1.0
|
||||||
|
>>> evaluate(["-1.1"])
|
||||||
|
-1.1
|
||||||
|
>>> evaluate(["2", "1", "+", "3", "*"])
|
||||||
|
9.0
|
||||||
|
>>> evaluate(["2", "1.9", "+", "3", "*"])
|
||||||
|
11.7
|
||||||
|
>>> evaluate(["2", "-1.9", "+", "3", "*"])
|
||||||
|
0.30000000000000027
|
||||||
|
>>> evaluate(["4", "13", "5", "/", "+"])
|
||||||
|
6.6
|
||||||
|
>>> evaluate(["2", "-", "3", "+"])
|
||||||
|
1.0
|
||||||
|
>>> evaluate(["-4", "5", "*", "6", "-"])
|
||||||
|
-26.0
|
||||||
|
>>> evaluate([])
|
||||||
|
0
|
||||||
|
>>> evaluate(["4", "-", "6", "7", "/", "9", "8"])
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ArithmeticError: Input is not a valid postfix expression
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
post_fix:
|
||||||
|
The postfix expression is tokenized into operators and operands and stored
|
||||||
|
as a Python list
|
||||||
|
|
||||||
|
verbose:
|
||||||
|
Display stack contents while evaluating the expression if verbose is True
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
float
|
||||||
|
The evaluated value
|
||||||
|
"""
|
||||||
|
if not post_fix:
|
||||||
return 0
|
return 0
|
||||||
# Checking the list to find out whether the postfix expression is valid
|
# Checking the list to find out whether the postfix expression is valid
|
||||||
for x in post_fix:
|
valid_expression = [parse_token(token) for token in post_fix]
|
||||||
valid_expression.append(get_number(x))
|
|
||||||
if verbose:
|
if verbose:
|
||||||
# print table header
|
# print table header
|
||||||
print("Symbol".center(8), "Action".center(12), "Stack", sep=" | ")
|
print("Symbol".center(8), "Action".center(12), "Stack", sep=" | ")
|
||||||
print("-" * (30 + len(post_fix)))
|
print("-" * (30 + len(post_fix)))
|
||||||
|
stack = []
|
||||||
for x in valid_expression:
|
for x in valid_expression:
|
||||||
if not is_operator(x):
|
if x not in OPERATORS:
|
||||||
stack.append(x) # append x to stack
|
stack.append(x) # append x to stack
|
||||||
if verbose:
|
if verbose:
|
||||||
# output in tabular format
|
# output in tabular format
|
||||||
print(
|
print(
|
||||||
str(x).rjust(8),
|
f"{x}".rjust(8),
|
||||||
("push(" + str(x) + ")").ljust(12),
|
f"push({x})".ljust(12),
|
||||||
stack,
|
stack,
|
||||||
sep=" | ",
|
sep=" | ",
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
# If x is operator
|
# If x is operator
|
||||||
# If only 1 value is inside stack and + or - is encountered
|
# If only 1 value is inside the stack and + or - is encountered
|
||||||
# then this is unary + or - case
|
# then this is unary + or - case
|
||||||
if x in UNARY_OP_SYMBOLS and len(stack) < 2:
|
if x in UNARY_OP_SYMBOLS and len(stack) < 2:
|
||||||
b = stack.pop() # pop stack
|
b = stack.pop() # pop stack
|
||||||
@ -150,13 +140,13 @@ def evaluate(post_fix: list[str], verbose: bool = False) -> int | float | str |
|
|||||||
# output in tabular format
|
# output in tabular format
|
||||||
print(
|
print(
|
||||||
"".rjust(8),
|
"".rjust(8),
|
||||||
("pop(" + str(b) + ")").ljust(12),
|
f"pop({b})".ljust(12),
|
||||||
stack,
|
stack,
|
||||||
sep=" | ",
|
sep=" | ",
|
||||||
)
|
)
|
||||||
print(
|
print(
|
||||||
str(x).rjust(8),
|
str(x).rjust(8),
|
||||||
("push(" + str(x) + str(b) + ")").ljust(12),
|
f"push({x}{b})".ljust(12),
|
||||||
stack,
|
stack,
|
||||||
sep=" | ",
|
sep=" | ",
|
||||||
)
|
)
|
||||||
@ -166,7 +156,7 @@ def evaluate(post_fix: list[str], verbose: bool = False) -> int | float | str |
|
|||||||
# output in tabular format
|
# output in tabular format
|
||||||
print(
|
print(
|
||||||
"".rjust(8),
|
"".rjust(8),
|
||||||
("pop(" + str(b) + ")").ljust(12),
|
f"pop({b})".ljust(12),
|
||||||
stack,
|
stack,
|
||||||
sep=" | ",
|
sep=" | ",
|
||||||
)
|
)
|
||||||
@ -176,59 +166,35 @@ def evaluate(post_fix: list[str], verbose: bool = False) -> int | float | str |
|
|||||||
# output in tabular format
|
# output in tabular format
|
||||||
print(
|
print(
|
||||||
"".rjust(8),
|
"".rjust(8),
|
||||||
("pop(" + str(a) + ")").ljust(12),
|
f"pop({a})".ljust(12),
|
||||||
stack,
|
stack,
|
||||||
sep=" | ",
|
sep=" | ",
|
||||||
)
|
)
|
||||||
# evaluate the 2 values popped from stack & push result to stack
|
# evaluate the 2 values popped from stack & push result to stack
|
||||||
stack.append(opr[str(x)](a, b))
|
stack.append(OPERATORS[x](a, b)) # type: ignore[index]
|
||||||
if verbose:
|
if verbose:
|
||||||
# output in tabular format
|
# output in tabular format
|
||||||
print(
|
print(
|
||||||
str(x).rjust(8),
|
f"{x}".rjust(8),
|
||||||
("push(" + str(a) + str(x) + str(b) + ")").ljust(12),
|
f"push({a}{x}{b})".ljust(12),
|
||||||
stack,
|
stack,
|
||||||
sep=" | ",
|
sep=" | ",
|
||||||
)
|
)
|
||||||
# else:
|
# If everything is executed correctly, the stack will contain
|
||||||
# msg = f"{x} is neither a number nor a valid operator"
|
|
||||||
# raise ValueError(msg)
|
|
||||||
# If everything executed correctly, the stack will contain
|
|
||||||
# only one element which is the result
|
# only one element which is the result
|
||||||
if len(stack) != 1:
|
if len(stack) != 1:
|
||||||
raise ArithmeticError("Input is not a valid postfix expression")
|
raise ArithmeticError("Input is not a valid postfix expression")
|
||||||
return stack[0]
|
return float(stack[0])
|
||||||
|
|
||||||
|
|
||||||
def is_yes(val: str) -> bool:
|
|
||||||
"""
|
|
||||||
Function that checks whether a user has entered any representation of a Yes (y, Y).
|
|
||||||
Any other input is considered as a No.
|
|
||||||
|
|
||||||
Parameters
|
|
||||||
-----------
|
|
||||||
val : str
|
|
||||||
The value entered by user
|
|
||||||
|
|
||||||
Returns
|
|
||||||
-------
|
|
||||||
bool
|
|
||||||
True if Yes, otherwise False
|
|
||||||
"""
|
|
||||||
return val in ("Y", "y")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
loop = True
|
# Create a loop so that the user can evaluate postfix expressions multiple times
|
||||||
# Creating a loop so that user can evaluate postfix expression multiple times
|
|
||||||
while True:
|
while True:
|
||||||
expression = input("Enter a Postfix Expression (space separated): ").split(" ")
|
expression = input("Enter a Postfix Expression (space separated): ").split(" ")
|
||||||
choice = input(
|
prompt = "Do you want to see stack contents while evaluating? [y/N]: "
|
||||||
"Do you want to see stack contents while evaluating? [y/N]: "
|
verbose = input(prompt).strip().lower() == "y"
|
||||||
).strip()
|
output = evaluate(expression, verbose)
|
||||||
display = is_yes(choice)
|
|
||||||
output = evaluate(expression, display)
|
|
||||||
print("Result = ", output)
|
print("Result = ", output)
|
||||||
choice = input("Do you want to enter another expression? [y/N]: ")
|
prompt = "Do you want to enter another expression? [y/N]: "
|
||||||
if not is_yes(choice):
|
if input(prompt).strip().lower() != "y":
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user