-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
add : to add generate_paranthese program under BACKTRACKING #9937
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
15
commits into
TheAlgorithms:master
from
kosuri-indu:add/generate_parentheses.py
Closed
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f5f5dec
to add generate_par.py
kosuri-indu c66c652
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 208d780
changes
kosuri-indu 7c33049
Merge branch 'add/generate_parentheses.py' of https://github.com/kosu…
kosuri-indu 48f81eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 075be7b
changes
kosuri-indu e6f638d
Merge branch 'add/generate_parentheses.py' of https://github.com/kosu…
kosuri-indu 6ff0a01
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3d2c1a0
changes
kosuri-indu 646a6fe
Merge branch 'add/generate_parentheses.py' of https://github.com/kosu…
kosuri-indu d093615
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8d60c2b
changes
kosuri-indu ba0e262
changes
kosuri-indu e0dd0ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 13e3ad0
changes
kosuri-indu 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,50 @@ | ||
""" | ||
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 = 0) -> list: | ||
""" | ||
>>> generate_parentheses(3) | ||
['((()))', '(()())', '(())()', '()(())', '()()()'] | ||
|
||
>>> generate_parentheses(1) | ||
['()'] | ||
|
||
>>> generate_parentheses(0) | ||
[''] | ||
""" | ||
def backtrack(parantheses : str = "",left : int = 0,right : int = 0) -> None: | ||
|
||
if len(parantheses) == 2 * number: | ||
result.append(parantheses) | ||
return | ||
if left < number: | ||
backtrack(parantheses + "(", left + 1, right) | ||
if right < left: | ||
backtrack(parantheses + ")", 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.