mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-26 02:48:40 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
5bbcee5c0d
commit
b4565e0f60
@ -68,7 +68,7 @@ def is_operator(data: str) -> bool:
|
|||||||
bool
|
bool
|
||||||
True if data is an operator else False.
|
True if data is an operator else False.
|
||||||
"""
|
"""
|
||||||
if data in ['-', '+', '*', '^', '/']:
|
if data in ["-", "+", "*", "^", "/"]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -120,39 +120,64 @@ def evaluate(post_fix: list, verbose: bool = False) -> int:
|
|||||||
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(str(x).rjust(8), ("push(" + str(x) + ")").ljust(12), stack, sep=" | ")
|
print(
|
||||||
|
str(x).rjust(8),
|
||||||
|
("push(" + str(x) + ")").ljust(12),
|
||||||
|
stack,
|
||||||
|
sep=" | ",
|
||||||
|
)
|
||||||
elif is_operator(x):
|
elif is_operator(x):
|
||||||
# If only 1 value is inside stack and + or - is encountered, then this is unary + or - case
|
# If only 1 value is inside stack and + or - is encountered, then this is unary + or - case
|
||||||
if x in ['-', '+'] and len(stack) < 2:
|
if x in ["-", "+"] and len(stack) < 2:
|
||||||
b = stack.pop() # pop stack
|
b = stack.pop() # pop stack
|
||||||
if x == '-':
|
if x == "-":
|
||||||
stack.append(-b) # negate b and push again into stack
|
stack.append(-b) # negate b and push again into stack
|
||||||
else: # when x is unary +
|
else: # when x is unary +
|
||||||
stack.append(b)
|
stack.append(b)
|
||||||
if verbose:
|
if verbose:
|
||||||
# output in tabular format
|
# output in tabular format
|
||||||
print("".rjust(8), ("pop(" + str(b) + ")").ljust(12), stack, sep=" | ")
|
print(
|
||||||
print(str(x).rjust(8), ("push(" + str(x) + str(b) + ")").ljust(12), stack, sep=" | ")
|
"".rjust(8), ("pop(" + str(b) + ")").ljust(12), stack, sep=" | "
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
str(x).rjust(8),
|
||||||
|
("push(" + str(x) + str(b) + ")").ljust(12),
|
||||||
|
stack,
|
||||||
|
sep=" | ",
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
b = stack.pop() # pop stack
|
b = stack.pop() # pop stack
|
||||||
if verbose:
|
if verbose:
|
||||||
# output in tabular format
|
# output in tabular format
|
||||||
print("".rjust(8), ("pop(" + str(b) + ")").ljust(12), stack, sep=" | ")
|
print(
|
||||||
|
"".rjust(8), ("pop(" + str(b) + ")").ljust(12), stack, sep=" | "
|
||||||
|
)
|
||||||
|
|
||||||
a = stack.pop() # pop stack
|
a = stack.pop() # pop stack
|
||||||
if verbose:
|
if verbose:
|
||||||
# output in tabular format
|
# output in tabular format
|
||||||
print("".rjust(8), ("pop(" + str(a) + ")").ljust(12), stack, sep=" | ")
|
print(
|
||||||
|
"".rjust(8), ("pop(" + str(a) + ")").ljust(12), stack, sep=" | "
|
||||||
|
)
|
||||||
|
|
||||||
stack.append(opr[x](a, b)) # evaluate the 2 values popped from stack & push result to stack
|
stack.append(
|
||||||
|
opr[x](a, b)
|
||||||
|
) # evaluate the 2 values popped from stack & push result to stack
|
||||||
if verbose:
|
if verbose:
|
||||||
# output in tabular format
|
# output in tabular format
|
||||||
print(str(x).rjust(8), ("push(" + str(a) + str(x) + str(b) + ")").ljust(12), stack, sep=" | ")
|
print(
|
||||||
|
str(x).rjust(8),
|
||||||
|
("push(" + str(a) + str(x) + str(b) + ")").ljust(12),
|
||||||
|
stack,
|
||||||
|
sep=" | ",
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print(f"{x} is neither a number, nor a valid operator")
|
print(f"{x} is neither a number, nor a valid operator")
|
||||||
break
|
break
|
||||||
if len(stack) == 1: # If everything executed correctly, the stack will contain only one element which is the result
|
if (
|
||||||
|
len(stack) == 1
|
||||||
|
): # If everything executed correctly, the stack will contain only one element which is the result
|
||||||
_, result = get_number(stack[0])
|
_, result = get_number(stack[0])
|
||||||
else:
|
else:
|
||||||
result = None
|
result = None
|
||||||
@ -174,7 +199,7 @@ def is_yes(val: str) -> bool:
|
|||||||
bool
|
bool
|
||||||
True if Yes, otherwise False
|
True if Yes, otherwise False
|
||||||
"""
|
"""
|
||||||
if val in ['Y', 'y']:
|
if val in ["Y", "y"]:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
@ -182,8 +207,12 @@ def is_yes(val: str) -> bool:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
loop = True
|
loop = True
|
||||||
while loop: # Creating a loop so that user can evaluate postfix expression multiple times
|
while (
|
||||||
expression = input("Enter a Postfix Equation (space separated) For Example: 5 6 9 * +\n: ").split(" ")
|
loop
|
||||||
|
): # Creating a loop so that user can evaluate postfix expression multiple times
|
||||||
|
expression = input(
|
||||||
|
"Enter a Postfix Equation (space separated) For Example: 5 6 9 * +\n: "
|
||||||
|
).split(" ")
|
||||||
choice = input("Do you want to see stack contents while evaluating? [y/N]: ")
|
choice = input("Do you want to see stack contents while evaluating? [y/N]: ")
|
||||||
display = is_yes(choice)
|
display = is_yes(choice)
|
||||||
output = evaluate(expression, display)
|
output = evaluate(expression, display)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user