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
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 35 additions & 34 deletions kyu_7/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,40 @@ rank - the harder the kata the faster you advance.

### List of Completed Kata (Python 3)
<!-- markdownlint-disable MD013 -->
| No. | Puzzle/Kata Name | Solution / GitHub Link |
|-----|:--------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------:|
| 1 | [Always perfect](https://www.codewars.com/kata/55f3facb78a9fd5b26000036) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/always_perfect) |
| 2 | [Beginner Series #3 Sum of Numbers](https://www.codewars.com/kata/55f2b110f61eb01779000053) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/beginner_series_sum_of_numbers) |
| 2 | [Coloured Triangles](https://www.codewars.com/kata/5a25ac6ac5e284cfbe000111) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/coloured_triangles) |
| 3 | [Disemvowel Trolls](https://www.codewars.com/kata/52fba66badcd10859f00097e) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/disemvowel_trolls) |
| 4 | [Factorial](https://www.codewars.com/kata/54ff0d1f355cfd20e60001fc) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/factorial) |
| 5 | [Computer problem series #1](https://www.codewars.com/kata/5d49c93d089c6e000ff8428c) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/fill_the_hard_disk_drive) |
| 6 | [Fun with lists: length](https://www.codewars.com/kata/581e476d5f59408553000a4b) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/fun_with_lists_length) |
| 7 | [Jaden Casing Strings](https://www.codewars.com/kata/5390bac347d09b7da40006f6) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/jaden_casing_strings) |
| 8 | [Make Class](https://www.codewars.com/kata/5d774cfde98179002a7cb3c8) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/make_class) |
| 9 | [Maximum Multiple](https://www.codewars.com/kata/5aba780a6a176b029800041c) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/maximum_multiple) |
| 10 | [Password validator](https://www.codewars.com/kata/56a921fa8c5167d8e7000053) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/password_validator) |
| 11 | [Powers of 3](https://www.codewars.com/kata/57be674b93687de78c0001d9) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/powers_of_3) |
| 12 | [Pull your words together, man!](https://www.codewars.com/kata/59ad7d2e07157af687000070) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/pull_your_words_together_man) |
| 13 | [The museum of incredible dull things](https://www.codewars.com/kata/563cf89eb4747c5fb100001b) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/remove_the_minimum) |
| 14 | [Share price](https://www.codewars.com/kata/5603a4dd3d96ef798f000068) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/share_prices) |
| 15 | [Significant Figures Challenge](https://www.codewars.com/kata/5d9fe0ace0aad7001290acb7) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/significant_figures) |
| 16 | [Simple Fun #152: Invite More Women](https://www.codewars.com/kata/58acfe4ae0201e1708000075) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/simple_fun_152) |
| 17 | [Sort Out The Men From Boys](https://www.codewars.com/kata/5af15a37de4c7f223e00012d) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sort_out_the_men_from_boys) |
| 18 | [Substituting Variables Into Strings: Padded Numbers](https://www.codewars.com/kata/51c89385ee245d7ddf000001) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/substituting_variables_into_strings_padded_numbers) |
| 19 | [Sum of powers of 2](https://www.codewars.com/kata/5d9f95424a336600278a9632) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sum_of_powers_of_2) |
| 20 | [Sum of Triangular Numbers](https://www.codewars.com/kata/580878d5d27b84b64c000b51) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sum_of_triangular_numbers) |
| 21 | [Sum of two lowest positive integers](https://www.codewars.com/kata/558fc85d8fd1938afb000014) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sum_of_two_lowest_int) |
| 22 | [The First Non Repeated Character In A String](https://www.codewars.com/kata/570f6436b29c708a32000826) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/the_first_non_repeated_character_in_string) |
| 23 | [V A P O R C O D E](https://www.codewars.com/kata/5966eeb31b229e44eb00007a) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/vaporcode) |
| 24 | [You're a square](https://www.codewars.com/kata/54c27a33fb7da0db0100040e) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/you_are_square) |
| 25 | [Find the longest gap!](https://www.codewars.com/kata/55b86beb1417eab500000051) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/find_the_longest_gap) |
| 26 | [Simple Fun #74: Growing Plant](https://www.codewars.com/kata/58941fec8afa3618c9000184) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/growing_plant) |
| 27 | [Basic Math (Add or Subtract)](https://www.codewars.com/kata/5809b62808ad92e31b000031) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/basic_math_add_or_subtract) |
| 28 | [Sum of odd numbers](https://www.codewars.com/kata/55fd2d567d94ac3bc9000064) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sum_of_odd_numbers) |
| 29 | [Help Bob count letters and digits](https://www.codewars.com/kata/5738f5ea9545204cec000155) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/help_bob_count_letters_and_digits) |
| 30 | [Make Class](https://www.codewars.com/kata/5d774cfde98179002a7cb3c8) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/make_class) |
| 30 | [Easy Line](https://www.codewars.com/kata/56e7d40129035aed6c000632) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/easy_line) |
| No. | Puzzle/Kata Name | Solution / GitHub Link |
|-----|:-------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------:|
| 1 | [Always perfect](https://www.codewars.com/kata/55f3facb78a9fd5b26000036) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/always_perfect) |
| 2 | [Beginner Series #3 Sum of Numbers](https://www.codewars.com/kata/55f2b110f61eb01779000053) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/beginner_series_sum_of_numbers) |
| 2 | [Coloured Triangles](https://www.codewars.com/kata/5a25ac6ac5e284cfbe000111) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/coloured_triangles) |
| 3 | [Disemvowel Trolls](https://www.codewars.com/kata/52fba66badcd10859f00097e) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/disemvowel_trolls) |
| 4 | [Factorial](https://www.codewars.com/kata/54ff0d1f355cfd20e60001fc) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/factorial) |
| 5 | [Computer problem series #1](https://www.codewars.com/kata/5d49c93d089c6e000ff8428c) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/fill_the_hard_disk_drive) |
| 6 | [Fun with lists: length](https://www.codewars.com/kata/581e476d5f59408553000a4b) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/fun_with_lists_length) |
| 7 | [Jaden Casing Strings](https://www.codewars.com/kata/5390bac347d09b7da40006f6) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/jaden_casing_strings) |
| 8 | [Make Class](https://www.codewars.com/kata/5d774cfde98179002a7cb3c8) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/make_class) |
| 9 | [Maximum Multiple](https://www.codewars.com/kata/5aba780a6a176b029800041c) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/maximum_multiple) |
| 10 | [Password validator](https://www.codewars.com/kata/56a921fa8c5167d8e7000053) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/password_validator) |
| 11 | [Powers of 3](https://www.codewars.com/kata/57be674b93687de78c0001d9) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/powers_of_3) |
| 12 | [Pull your words together, man!](https://www.codewars.com/kata/59ad7d2e07157af687000070) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/pull_your_words_together_man) |
| 13 | [The museum of incredible dull things](https://www.codewars.com/kata/563cf89eb4747c5fb100001b) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/remove_the_minimum) |
| 14 | [Share price](https://www.codewars.com/kata/5603a4dd3d96ef798f000068) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/share_prices) |
| 15 | [Significant Figures Challenge](https://www.codewars.com/kata/5d9fe0ace0aad7001290acb7) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/significant_figures) |
| 16 | [Simple Fun #152: Invite More Women](https://www.codewars.com/kata/58acfe4ae0201e1708000075) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/simple_fun_152) |
| 17 | [Sort Out The Men From Boys](https://www.codewars.com/kata/5af15a37de4c7f223e00012d) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sort_out_the_men_from_boys) |
| 18 | [Substituting Variables Into Strings: Padded Numbers](https://www.codewars.com/kata/51c89385ee245d7ddf000001) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/substituting_variables_into_strings_padded_numbers) |
| 19 | [Sum of powers of 2](https://www.codewars.com/kata/5d9f95424a336600278a9632) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sum_of_powers_of_2) |
| 20 | [Sum of Triangular Numbers](https://www.codewars.com/kata/580878d5d27b84b64c000b51) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sum_of_triangular_numbers) |
| 21 | [Sum of two lowest positive integers](https://www.codewars.com/kata/558fc85d8fd1938afb000014) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sum_of_two_lowest_int) |
| 22 | [The First Non Repeated Character In A String](https://www.codewars.com/kata/570f6436b29c708a32000826) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/the_first_non_repeated_character_in_string) |
| 23 | [V A P O R C O D E](https://www.codewars.com/kata/5966eeb31b229e44eb00007a) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/vaporcode) |
| 24 | [You're a square](https://www.codewars.com/kata/54c27a33fb7da0db0100040e) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/you_are_square) |
| 25 | [Find the longest gap!](https://www.codewars.com/kata/55b86beb1417eab500000051) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/find_the_longest_gap) |
| 26 | [Simple Fun #74: Growing Plant](https://www.codewars.com/kata/58941fec8afa3618c9000184) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/growing_plant) |
| 27 | [Basic Math (Add or Subtract)](https://www.codewars.com/kata/5809b62808ad92e31b000031) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/basic_math_add_or_subtract) |
| 28 | [Sum of odd numbers](https://www.codewars.com/kata/55fd2d567d94ac3bc9000064) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/sum_of_odd_numbers) |
| 29 | [Help Bob count letters and digits](https://www.codewars.com/kata/5738f5ea9545204cec000155) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/help_bob_count_letters_and_digits) |
| 30 | [Make Class](https://www.codewars.com/kata/5d774cfde98179002a7cb3c8) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/make_class) |
| 30 | [Easy Line](https://www.codewars.com/kata/56e7d40129035aed6c000632) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/easy_line) |
| 31 | [Valid Parentheses](https://www.codewars.com/kata/6411b91a5e71b915d237332d) | [Solution](https://github.com/ikostan/codewars/tree/master/kyu_7/valid_parentheses) |
<!-- markdownlint-enable MD013 -->
[Source](https://www.codewars.com/about)
26 changes: 26 additions & 0 deletions kyu_7/valid_parentheses/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Valid Parentheses

Write a function that takes a string of parentheses, and determines
if the order of the parentheses is valid. The function should return
true if the string is valid, and false if it's invalid.

Examples:
```bash
"()" => true
")(()))" => false
"(" => false
"(())((()())())" => true
```

Constraints

```bash 0 <= length of input <= 100```

- All inputs will be strings, consisting only of characters ( and ).
- Empty strings are considered balanced (and therefore valid), and will be tested.
- For languages with mutable strings, the inputs should not be mutated.

*Tip:* If you are trying to figure out why a string of parentheses is invalid,
paste the parentheses into the code editor, and let the code highlighting show you!

[Source](https://www.codewars.com/kata/6411b91a5e71b915d237332d)
Empty file.
52 changes: 52 additions & 0 deletions kyu_7/valid_parentheses/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Solution for -> Valid Parentheses
Created by Egor Kostan.
GitHub: https://github.com/ikostan
"""

def valid_parentheses(paren_str: str) -> bool:
ikostan marked this conversation as resolved.
Show resolved Hide resolved
ikostan marked this conversation as resolved.
Show resolved Hide resolved
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

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.

"""
A function that takes a string of parentheses, and determines
if the order of the parentheses is valid. The function should
return true if the string is valid, and false if it's invalid.
:param paren_str: str
:return: bool
"""
print(paren_str)

# Should return True for empty strings
if not paren_str:
return True

# Fail if starts from closing bracket
if paren_str[0] == ')':
return False

# A number of closing and opening
# brackets should be equal
if paren_str.count('(') != paren_str.count(')'):
return False

while paren_str:
# Fail if starts from closing bracket
if paren_str[0] == ')':
return False
# Find matching pair and remove them from the string
for i in range(1, len(paren_str)):
if paren_str[i] == ')':
paren_str = update_str(paren_str, i)
break

return True
ikostan marked this conversation as resolved.
Show resolved Hide resolved


def update_str(paren_str: str, i: int) -> str:
"""
Update string:
1. remove first char.

Check notice on line 46 in kyu_7/valid_parentheses/solution.py

View check run for this annotation

codefactor.io / CodeFactor

kyu_7/valid_parentheses/solution.py#L46

Unnecessary list index lookup, use 'char' instead (unnecessary-list-index-lookup)
2. remove matching char/bracket.
:param paren_str: str
:param i: int
:return: str
"""
return ''.join(paren_str[indx] for indx, char in enumerate(paren_str) if indx not in [0, i])

Check notice on line 52 in kyu_7/valid_parentheses/solution.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

kyu_7/valid_parentheses/solution.py#L52

Unnecessary list index lookup, use 'char' instead
Loading
Loading