From 9a15148f8becc9616165e3581a6b88588c2eecbe Mon Sep 17 00:00:00 2001 From: Arijit De Date: Wed, 23 Aug 2023 16:01:23 +0530 Subject: [PATCH] Fixes #8754, #8724 Updated the parse_token function of postfix_evaluation.py ostfix_evaluation.py now supports Unary operators and floating point numbers. Also merged evaluate_postfix_notations.py and postfix_evaluation.py into postfix_evaluation.py which fixes #8724. Added a doctest example with unary operator and invalid expression. --- data_structures/stacks/postfix_evaluation.py | 23 ++++++++------------ 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/data_structures/stacks/postfix_evaluation.py b/data_structures/stacks/postfix_evaluation.py index bc65052c0..c3a0024f4 100644 --- a/data_structures/stacks/postfix_evaluation.py +++ b/data_structures/stacks/postfix_evaluation.py @@ -31,7 +31,7 @@ UNARY_OP_SYMBOLS = ("-", "+") BINARY_OP_SYMBOLS = ("-", "+", "*", "^", "/") -def parse_token(data: str) -> float | str: +def parse_token(token: str | float) -> 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 @@ -39,26 +39,21 @@ def parse_token(data: str) -> float | str: Parameters ---------- - data : str + token : str or float 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' + float or str + Returns a float if `token` is a number and returns `token` if it's an operator """ + if is_operator(token): + return token try: - return int(data) + return float(token) 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) + msg = f"{token} is neither a number nor a valid operator" + raise ValueError(msg) def is_operator(token: str | float) -> bool: