Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Valid Parentheses - Merge pull request #543 from iKostanOrg/master #544

Merged
merged 21 commits into from
Dec 1, 2024

Conversation

ikostan
Copy link
Member

@ikostan ikostan commented Nov 30, 2024

Merge from master

Summary by Sourcery

Add a new solution for the 'Valid Parentheses' problem, including comprehensive unit tests and documentation.

New Features:

  • Add a new solution for the 'Valid Parentheses' problem, which checks if the order of parentheses in a string is valid.

Documentation:

  • Add documentation for the 'Valid Parentheses' problem, including a description, examples, and constraints.

Tests:

  • Introduce unit tests for the 'Valid Parentheses' function to verify its correctness with various test cases.

@ikostan ikostan added documentation Improvements or additions to documentation codewars kyu_7 labels Nov 30, 2024
@ikostan ikostan self-assigned this Nov 30, 2024
Copy link
Contributor

sourcery-ai bot commented Nov 30, 2024

Reviewer's Guide by Sourcery

This pull request adds a new kata solution for "Valid Parentheses" problem and updates the README.md file to include it in the list of completed katas. The implementation includes a solution file and comprehensive test cases to verify the functionality.

Class diagram for Valid Parentheses solution

classDiagram
    class ValidParenthesesTestCase {
        +test_valid_parentheses_true()
        +test_valid_parentheses_false()
        +test_valid_parentheses_empty_string()
        +test_valid_parentheses_large_valid()
        +test_valid_parentheses_large_invalid()
    }
    class valid_parentheses {
        +valid_parentheses(paren_str: str) bool
    }
    class update_str {
        +update_str(paren_str: str, i: int) str
    }
    ValidParenthesesTestCase --> valid_parentheses
    valid_parentheses --> update_str
    note for ValidParenthesesTestCase "Unit tests for the valid_parentheses function"
Loading

File-Level Changes

Change Details Files
Added solution for Valid Parentheses kata
  • Implemented valid_parentheses function that validates parentheses string order
  • Added helper function update_str to handle string manipulation
  • Included edge case handling for empty strings and invalid patterns
kyu_7/valid_parentheses/solution.py
Added comprehensive test suite for Valid Parentheses solution
  • Created test cases for valid parentheses strings
  • Added test cases for invalid parentheses strings
  • Included tests for empty strings
  • Added tests for large valid and invalid strings
kyu_7/valid_parentheses/test_valid_parentheses.py
Updated documentation and project structure
  • Added README.md with problem description and constraints
  • Updated main README.md to include new kata in the list
  • Added proper documentation and type hints to solution code
kyu_7/valid_parentheses/README.md
kyu_7/README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have skipped reviewing this pull request. Here's why:

  • We don't review packaging changes - Let us know if you'd like us to change this.
  • All of the files are larger than we can process. We're working on it!

Copy link

codecov bot commented Nov 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.01%. Comparing base (ab7708c) to head (d87e55d).
Report is 22 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #544   +/-   ##
=======================================
  Coverage   90.01%   90.01%           
=======================================
  Files         171      171           
  Lines        2574     2574           
=======================================
  Hits         2317     2317           
  Misses        257      257           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

codeclimate bot commented Nov 30, 2024

Code Climate has analyzed commit d87e55d and detected 1 issue on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 1

The test coverage on the diff in this pull request is 100.0% (50% is the threshold).

This pull request will bring the total coverage in the repository to 90.0% (0.0% change).

View more on Code Climate.

kyu_7/valid_parentheses/solution.py Show resolved Hide resolved
kyu_7/valid_parentheses/solution.py Show resolved Hide resolved
kyu_7/valid_parentheses/test_valid_parentheses.py Outdated Show resolved Hide resolved
kyu_7/valid_parentheses/solution.py Show resolved Hide resolved
kyu_7/valid_parentheses/test_valid_parentheses.py Outdated Show resolved Hide resolved
Missing module docstring
@ikostan
Copy link
Member Author

ikostan commented Nov 30, 2024

@sourcery-ai

@ikostan
Copy link
Member Author

ikostan commented Nov 30, 2024

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ikostan - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

kyu_7/valid_parentheses/test_valid_parentheses.py Outdated Show resolved Hide resolved
"""


def valid_parentheses(paren_str: str) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider using a counter-based approach instead of string manipulation to validate parentheses

The current implementation unnecessarily rebuilds strings and uses nested loops, leading to O(n²) complexity. This can be simplified to O(n) using a counter approach that better expresses the intent:

def valid_parentheses(paren_str: str) -> bool:
    count = 0
    for char in paren_str:
        if char == '(':
            count += 1
        else:
            count -= 1
            if count < 0:  # Too many closing brackets
                return False
    return count == 0

This solution:

  • Eliminates string rebuilding and nested loops
  • Maintains the same functionality with better performance
  • Is more readable as it directly expresses the logic of matching pairs

Comment on lines 56 to 61
for test_str in test_data:
with allure.step(f"Enter test string {test_data}"
f"and verify that the function returns True."):
result: bool = valid_parentheses(test_str)
print_log(test_data=test_data, result=result)
self.assertTrue(result)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Avoid loops in tests. (no-loop-in-tests)

ExplanationAvoid complex code, like loops, in test functions.

Google's software engineering guidelines says:
"Clear tests are trivially correct upon inspection"
To reach that avoid complex code in tests:

  • loops
  • conditionals

Some ways to fix this:

  • Use parametrized tests to get rid of the loop.
  • Move the complex logic into helpers.
  • Move the complex part into pytest fixtures.

Complexity is most often introduced in the form of logic. Logic is defined via the imperative parts of programming languages such as operators, loops, and conditionals. When a piece of code contains logic, you need to do a bit of mental computation to determine its result instead of just reading it off of the screen. It doesn't take much logic to make a test more difficult to reason about.

Software Engineering at Google / Don't Put Logic in Tests

kyu_7/valid_parentheses/test_valid_parentheses.py Outdated Show resolved Hide resolved
kyu_7/valid_parentheses/test_valid_parentheses.py Outdated Show resolved Hide resolved
kyu_7/valid_parentheses/test_valid_parentheses.py Outdated Show resolved Hide resolved
kyu_7/valid_parentheses/test_valid_parentheses.py Outdated Show resolved Hide resolved
kyu_7/valid_parentheses/solution.py:53:19: R1736: Unnecessary list index lookup, use 'char' instead (unnecessary-list-index-lookup)
kyu_7/valid_parentheses/test_valid_parentheses.py:108:25: W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
kyu_7/valid_parentheses/test_valid_parentheses.py:130:26: W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
kyu_7/valid_parentheses/test_valid_parentheses.py:132:29: W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
kyu_7/valid_parentheses/test_valid_parentheses.py:154:26: W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
kyu_7/valid_parentheses/test_valid_parentheses.py:156:29: W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
************* Module kyu_7.valid_parentheses.test_valid_parentheses
kyu_7/valid_parentheses/test_valid_parentheses.py:130:26: W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
kyu_7/valid_parentheses/test_valid_parentheses.py:154:26: W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
"""


def valid_parentheses(paren_str: str) -> bool:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function valid_parentheses has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.

issue (bug_risk): Fix incorrect parameter in print_log call

The print_log is using test_data instead of test_str which could cause confusing log output. Should be print_log(test_data=test_str, result=result)
@ikostan ikostan merged commit f128538 into master Dec 1, 2024
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codewars documentation Improvements or additions to documentation kyu_7
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant