From 8a8a2486c4f280e80c0676f5622c0d51a2b4de14 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Tue, 29 Oct 2024 20:52:28 -0700 Subject: [PATCH] Update evaluate.py --- .../evaluate.py | 74 +++++++++++-------- 1 file changed, 43 insertions(+), 31 deletions(-) diff --git a/kyu_2/evaluate_mathematical_expression/evaluate.py b/kyu_2/evaluate_mathematical_expression/evaluate.py index ff6417f107d..1bc79d36b28 100644 --- a/kyu_2/evaluate_mathematical_expression/evaluate.py +++ b/kyu_2/evaluate_mathematical_expression/evaluate.py @@ -136,7 +136,7 @@ def process_duplicate_minus(string: str) -> str: del strings[i] break - return ' '.join([s for s in strings if s != '']) + return ' '.join(strings) def calc(string: str) -> float: @@ -158,44 +158,56 @@ def calc(string: str) -> float: return float(string) -def normalize_string(string: str) -> str: +def check_conditions(strings, string, temp) -> (str, str): """ - Normalizing string input - :param string: str - :return: str + Check conditions + :param strings: + :param string: + :param temp: + :return: """ - strings: list = [] + for i, s in enumerate(string): + if s.isdigit(): + temp += s - while string != '': - temp: str = '' + if s in '()': + if temp != '': + strings.append(temp) + strings.append(s) - for i, s in enumerate(string): - if s.isdigit(): - temp += s + if i + 1 < len(string): + string = string[i + 1:] + else: + string = '' + break - if s in '()': - if temp != '': - strings.append(temp) - strings.append(s) + if s in OPERATORS: + if temp != '': + strings.append(temp) + strings.append(s) - if i + 1 < len(string): - string = string[i + 1:] - else: - string = '' - break + if i + 1 < len(string): + string = string[i + 1:] + break - if s in OPERATORS: - if temp != '': - strings.append(temp) - strings.append(s) + if i == len(string) - 1: + if temp != '': + strings.append(temp) + string = '' - if i + 1 < len(string): - string = string[i + 1:] - break + return temp, string - if i == len(string) - 1: - if temp != '': - strings.append(temp) - string = '' + +def normalize_string(string: str) -> str: + """ + Normalizing string input + :param string: str + :return: str + """ + strings: list = [] + + while string: + temp: str = '' + temp, string = check_conditions(strings, string, temp) return ' '.join(strings)