diff --git a/.github/workflows/flake8_kyu2.yml b/.github/workflows/flake8_kyu2.yml index 0ba0c59bfab..3f848fe08e3 100644 --- a/.github/workflows/flake8_kyu2.yml +++ b/.github/workflows/flake8_kyu2.yml @@ -42,6 +42,6 @@ jobs: # exit-zero treats all errors as warnings. # The GitHub editor is 127 chars wide run: | - flake8 /kyu_2 --count --select=E9,F63,F7,F82 --doctests --show-source --statistics - flake8 . --count --max-complexity=10 --max-line-length=127 --benchmark --show-source --statistics + flake8 --count --select=E9,F63,F7,F82 --doctests --show-source --statistics ./kyu_2 + flake8 --count --max-complexity=10 --max-line-length=127 --benchmark --show-source --statistics ./kyu_2 # yamllint enable rule:line-length \ No newline at end of file diff --git a/kyu_2/evaluate_mathematical_expression/evaluate.py b/kyu_2/evaluate_mathematical_expression/evaluate.py index ed24d33d52b..6c297d42e48 100644 --- a/kyu_2/evaluate_mathematical_expression/evaluate.py +++ b/kyu_2/evaluate_mathematical_expression/evaluate.py @@ -51,50 +51,6 @@ def process_math_expression(string: str, operators: list) -> str: return ' '.join(strings) -def normalize_string(string: str) -> str: - """ - Normalizing string input - :param string: str - :return: str - """ - strings: list = [] - string_temp: str = ''.join([s for s in string if s != ' ']) - - while string_temp != '': - temp: str = '' - - for i, s in enumerate(string_temp): - if s.isdigit(): - temp += s - - if s in '()': - if temp != '': - strings.append(temp) - strings.append(s) - - if i + 1 < len(string_temp): - string_temp = string_temp[i + 1:] - else: - string_temp = '' - break - - if s in OPERATORS: - if temp != '': - strings.append(temp) - strings.append(s) - - if i + 1 < len(string_temp): - string_temp = string_temp[i + 1:] - break - - if i == len(string_temp) - 1: - if temp != '': - strings.append(temp) - string_temp = '' - - return ' '.join([s for s in strings if s != '']) - - def bracket_start(strings: list) -> int: """ Return index of first (open) bracket @@ -180,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: @@ -189,9 +145,16 @@ def calc(string: str) -> float: :param string: str :return: float """ - string = normalize_string(string) + string = ''.join([s for s in string if s != ' ']) + + strings: list = [] + while string: + temp: str = '' + temp, string = check_conditions(strings, string, temp) + string = ' '.join(strings) + string = ''.join(string.split('+')) - strings: list = string.split() + strings = string.split() string = process_brackets(strings) string = process_duplicate_minus(string) string = process_math_expression(string, ['*', '/']) @@ -199,3 +162,42 @@ def calc(string: str) -> float: string_lst = [float(s) for s in string_lst] string = str(sum(string_lst)) return float(string) + + +def check_conditions(strings: list, string: str, temp: str) -> tuple[str, str]: + """ + Normalizing string input by checking conditions + :param strings: list + :param string: str + :param temp: str + :return: tuple(str, str) + """ + for i, s in enumerate(string): + if s.isdigit(): + temp += s + + if (s in ''.join(OPERATORS) + '()' or i == len(string) - 1) and temp: + strings.append(temp) + + if s in '()': + strings.append(s) + + if i + 1 < len(string): + string = string[i + 1:] + else: + string = '' + + break + + if s in OPERATORS: + strings.append(s) + + if i + 1 < len(string): + string = string[i + 1:] + + break + + if i == len(string) - 1: + string = '' + + return temp, string diff --git a/kyu_2/evaluate_mathematical_expression/test_evaluate.py b/kyu_2/evaluate_mathematical_expression/test_evaluate.py index 9851b41495b..cb9f8073717 100644 --- a/kyu_2/evaluate_mathematical_expression/test_evaluate.py +++ b/kyu_2/evaluate_mathematical_expression/test_evaluate.py @@ -78,8 +78,7 @@ def test_calc(self): ['-(-93) / (-36 + 26 + -(18)) + (-7 * -(((-(-67 + -95)))) + -9)', 1121.6785714285713], ['-(-23) + (-4 * -13 + -(1)) - (-30 / (((-(57 + -20)))) + 85)', -11.810810810810807], ['(72) / (-82 - -93 * -(88)) + (-18 - -(((-(60 * 97)))) + -79)', -5917.00871037987], - ['-(77) / (7 * -76 + (59)) + (98 / -(((-(-74 - -47)))) / -5)', 0.8887166236003445] - ) + ['-(77) / (7 * -76 + (59)) + (98 / -(((-(-74 - -47)))) / -5)', 0.8887166236003445]) for string, expected in test_data: