Skip to content

Commit

Permalink
Merge pull request #510 from iKostanOrg/kyu2
Browse files Browse the repository at this point in the history
Merge pull request #509 from iKostanOrg/master
  • Loading branch information
ikostan authored Oct 30, 2024
2 parents 1e44093 + 25db014 commit eb521f1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 51 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/flake8_kyu2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
96 changes: 49 additions & 47 deletions kyu_2/evaluate_mathematical_expression/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -189,13 +145,59 @@ 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, ['*', '/'])
string_lst: list = string.split(' ')
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
3 changes: 1 addition & 2 deletions kyu_2/evaluate_mathematical_expression/test_evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit eb521f1

Please sign in to comment.