From 9d506c454e1689ad2576299ebbd3602fa92bf40f Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Tue, 30 Sep 2025 13:27:51 -0700 Subject: [PATCH 1/6] Update wordy.py --- wordy/wordy.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/wordy/wordy.py b/wordy/wordy.py index da43570..1766bb9 100644 --- a/wordy/wordy.py +++ b/wordy/wordy.py @@ -42,26 +42,22 @@ def answer(question: str) -> int: :raises ValueError: If the operation is unknown or the syntax is invalid. """ _validate_errors(question) - result: int = 0 new_question: list[str] = _reformat(question) # Reduce iteratively: evaluate the first three-token slice # and fold the result left-to-right. - result: int = 0 while new_question: try: if len(new_question) == 3: _validate_evaluation_pattern(new_question) - result = _math_operation(new_question) - break + return _math_operation(new_question) if len(new_question) == 1: - result = int(new_question[0]) - break + return int(new_question[0]) _validate_evaluation_pattern(new_question[:3]) result = _math_operation(new_question[:3]) new_question = [str(result)] + new_question[3:] except Exception as exc: raise ValueError("syntax error") from exc - return result + raise ValueError("syntax error") # Safety net for empty cases def _math_operation(question: list[str]) -> int: @@ -88,17 +84,19 @@ def _math_operation(question: list[str]) -> int: result = int(question[0]) // int(question[-1]) elif math_operator == "*": result = int(question[0]) * int(question[-1]) + else: + raise ValueError("syntax error") return result -def _validate_evaluation_pattern(val: list) -> None: +def _validate_evaluation_pattern(val: list[str]) -> None: """ Ensure a token slice matches expected evaluation patterns. :param val: Token slice to validate, e.g., ['3', '+', '4'] or ['+', '4'] during reduction. - :type val: list + :type val: list[str] :raises ValueError: If the pattern is invalid (syntax error). """ if len(val) == 3 and val[1] not in STR_TO_OPERATOR.values(): @@ -108,7 +106,7 @@ def _validate_evaluation_pattern(val: list) -> None: raise ValueError("syntax error") -def _reformat(question: str) -> list: +def _reformat(question: str) -> list[str]: """ Tokenize a natural-language math question into numbers and operator symbols. @@ -126,9 +124,9 @@ def _reformat(question: str) -> list: for item in question_list: if not ( item.isdigit() + or item[1:].isdigit() or item in STR_TO_OPERATOR or item in STR_TO_OPERATOR.values() - or any(val in item for val in STR_TO_OPERATOR.values()) ): continue From b997eb5152c768ffe463445d640b0dfd7a225d9d Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Tue, 30 Sep 2025 13:29:29 -0700 Subject: [PATCH 2/6] Update lint_test_report.yml --- .github/workflows/lint_test_report.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/lint_test_report.yml b/.github/workflows/lint_test_report.yml index 8e0813c..bb8f08c 100644 --- a/.github/workflows/lint_test_report.yml +++ b/.github/workflows/lint_test_report.yml @@ -46,7 +46,6 @@ jobs: name: "Ruff Lint and Format" needs: - "changed-files" - - "yamllint" if: needs.changed-files.outputs.python_changed == 'true' uses: "./.github/workflows/ruff.yml" @@ -54,7 +53,6 @@ jobs: name: "Pylint Workflow" needs: - "changed-files" - - "yamllint" if: needs.changed-files.outputs.python_changed == 'true' uses: "./.github/workflows/pylint.yml" @@ -62,7 +60,6 @@ jobs: name: "Flake8 Workflow" needs: - "changed-files" - - "yamllint" if: needs.changed-files.outputs.python_changed == 'true' uses: "./.github/workflows/flake8.yml" @@ -72,6 +69,7 @@ jobs: - "ruff" - "pylint" - "flake8" + - "yamllint" uses: "./.github/workflows/pytest.yml" codecov: From 15d056cdbd7e43ec0c6cea7bd5eca19ebe5f7926 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Tue, 30 Sep 2025 13:31:52 -0700 Subject: [PATCH 3/6] Update lint_test_report.yml --- .github/workflows/lint_test_report.yml | 34 -------------------------- 1 file changed, 34 deletions(-) diff --git a/.github/workflows/lint_test_report.yml b/.github/workflows/lint_test_report.yml index bb8f08c..df350fc 100644 --- a/.github/workflows/lint_test_report.yml +++ b/.github/workflows/lint_test_report.yml @@ -12,55 +12,21 @@ permissions: pull-requests: "read" jobs: - changed-files: - runs-on: "ubuntu-latest" - outputs: - python_changed: ${{ steps.python-files.outputs.any_changed }} - yaml_changed: ${{ steps.yaml-files.outputs.any_changed }} - steps: - - uses: "actions/checkout@v5" - - name: "Get changed Python files" - id: python-files - uses: "tj-actions/changed-files@v47" - with: - files: | - **/*.py - requirements.txt - .pylintrc - - name: "Get changed YAML files" - id: yaml-files - uses: "tj-actions/changed-files@v47" - with: - files: | - **/*.yml - **/*.yaml - yamllint: name: "YAML Lint Workflow" - needs: - - "changed-files" - if: needs.changed-files.outputs.yaml_changed == 'true' uses: "./.github/workflows/yamllint.yml" ruff: name: "Ruff Lint and Format" - needs: - - "changed-files" - if: needs.changed-files.outputs.python_changed == 'true' uses: "./.github/workflows/ruff.yml" pylint: name: "Pylint Workflow" - needs: - - "changed-files" - if: needs.changed-files.outputs.python_changed == 'true' uses: "./.github/workflows/pylint.yml" flake8: name: "Flake8 Workflow" needs: - - "changed-files" - if: needs.changed-files.outputs.python_changed == 'true' uses: "./.github/workflows/flake8.yml" pytest: From ad2dc239734873d56b03ed3d961024d95d89f58a Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Tue, 30 Sep 2025 13:32:26 -0700 Subject: [PATCH 4/6] Update lint_test_report.yml --- .github/workflows/lint_test_report.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/lint_test_report.yml b/.github/workflows/lint_test_report.yml index df350fc..d9b6126 100644 --- a/.github/workflows/lint_test_report.yml +++ b/.github/workflows/lint_test_report.yml @@ -26,7 +26,6 @@ jobs: flake8: name: "Flake8 Workflow" - needs: uses: "./.github/workflows/flake8.yml" pytest: From bce70f64d8a0e5101d03ab27bf5c8312df396861 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Tue, 30 Sep 2025 13:33:33 -0700 Subject: [PATCH 5/6] Update lint_test_report.yml --- .github/workflows/lint_test_report.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/lint_test_report.yml b/.github/workflows/lint_test_report.yml index d9b6126..7aeba23 100644 --- a/.github/workflows/lint_test_report.yml +++ b/.github/workflows/lint_test_report.yml @@ -19,14 +19,20 @@ jobs: ruff: name: "Ruff Lint and Format" uses: "./.github/workflows/ruff.yml" + needs: + - "yamllint" pylint: name: "Pylint Workflow" - uses: "./.github/workflows/pylint.yml" + uses: + needs: + - "yamllint" flake8: name: "Flake8 Workflow" uses: "./.github/workflows/flake8.yml" + needs: + - "yamllint" pytest: name: "Pytest Workflow" @@ -34,7 +40,6 @@ jobs: - "ruff" - "pylint" - "flake8" - - "yamllint" uses: "./.github/workflows/pytest.yml" codecov: From ca0dc0de46b2d3023587946e4006b1b299e06809 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Tue, 30 Sep 2025 13:34:25 -0700 Subject: [PATCH 6/6] Update lint_test_report.yml --- .github/workflows/lint_test_report.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint_test_report.yml b/.github/workflows/lint_test_report.yml index 7aeba23..f822b8f 100644 --- a/.github/workflows/lint_test_report.yml +++ b/.github/workflows/lint_test_report.yml @@ -24,7 +24,7 @@ jobs: pylint: name: "Pylint Workflow" - uses: + uses: "./.github/workflows/pylint.yml" needs: - "yamllint"