-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
add : to add generate_parentheses.py under backtracking #9922
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
Closed
kosuri-indu
wants to merge
20
commits into
TheAlgorithms:master
from
kosuri-indu:add/generate_parentheses.py
Closed
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
841f737
to add generate_parentheses.py
kosuri-indu e544013
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1b085f0
to add ->None
kosuri-indu 0bc793a
Merge branch 'add/generate_parentheses.py' of https://github.com/kosu…
kosuri-indu 5b96f96
to add ->None
kosuri-indu cfe859c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 583e8d3
to remove ->None
kosuri-indu e753986
changes
kosuri-indu 9f847d6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 63052db
changes
kosuri-indu 3d1dee9
Merge branch 'add/generate_parentheses.py' of https://github.com/kosu…
kosuri-indu 9c09f35
changes
kosuri-indu 087ede3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 686bfa9
changes
kosuri-indu 3e5bbc2
Merge branch 'add/generate_parentheses.py' of https://github.com/kosu…
kosuri-indu 9100faf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c05de07
changes
kosuri-indu 6d99cf1
Merge branch 'add/generate_parentheses.py' of https://github.com/kosu…
kosuri-indu aaa803a
changes
kosuri-indu 5ddbff0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Given 'n' pairs of parentheses, | ||
this program generates all combinations of parentheses. | ||
|
||
Example, n = 3 : | ||
[ | ||
"((()))", | ||
"(()())", | ||
"(())()", | ||
"()(())", | ||
"()()()" | ||
] | ||
|
||
This problem can be solved using the concept of "BACKTRACKING". | ||
|
||
By adding an open parenthesis to the solution and | ||
recursively add more till we get 'n' open parentheses. | ||
|
||
Then we start adding close parentheses until the solution is valid | ||
(open parenthesis is closed). | ||
|
||
If we reach a point where we can not add more parentheses to the solution, | ||
we backtrack to the previous step and try a different path. | ||
""" | ||
|
||
|
||
def generate_parentheses(number : int = 3): | ||
kosuri-indu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
>>> generate_parentheses(3) | ||
['((()))', '(()())', '(())()', '()(())', '()()()'] | ||
|
||
>>> generate_parentheses(3) | ||
['(())', '()()'] | ||
|
||
>>> generate_parentheses(1) | ||
['()'] | ||
|
||
>>> generate_parentheses(0) | ||
[''] | ||
|
||
""" | ||
|
||
def backtrack(x : str = "", left : int = 0, right : int = 0): | ||
kosuri-indu marked this conversation as resolved.
Show resolved
Hide resolved
kosuri-indu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if len(x) == 2 * number: | ||
result.append(x) | ||
return | ||
if left < number: | ||
backtrack(x + "(", left + 1, right) | ||
if right < left: | ||
backtrack(x + ")", left, right + 1) | ||
|
||
result = [] | ||
backtrack() | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
print(f"{generate_parentheses() = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.