-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
feat: add well-formed parentheses generator #2445
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a83d0cf
feat: add well-formed parentheses generator
giuseppe-coco ffeb915
updating DIRECTORY.md
d74a6a5
changes made
giuseppe-coco 4964558
Merge branch 'master' of https://github.com/WoWS17/C-Plus-Plus
giuseppe-coco e9bc5dc
Update backtracking/generate_parentheses.cpp
giuseppe-coco 46c4cc1
Update backtracking/generate_parentheses.cpp
giuseppe-coco efca21d
Update backtracking/generate_parentheses.cpp
giuseppe-coco 9109ced
Update backtracking/generate_parentheses.cpp
giuseppe-coco 6f790f3
Update backtracking/generate_parentheses.cpp
giuseppe-coco a8209d2
Update backtracking/generate_parentheses.cpp
giuseppe-coco 5029e71
Update backtracking/generate_parentheses.cpp
giuseppe-coco e1fafd0
Update backtracking/generate_parentheses.cpp
giuseppe-coco 3264a41
Update backtracking/generate_parentheses.cpp
giuseppe-coco 352cf4d
Update backtracking/generate_parentheses.cpp
giuseppe-coco 18a0851
Update backtracking/generate_parentheses.cpp
giuseppe-coco 3b247a6
chore: apply suggestions from code review
Panquesito7 728ddde
Update backtracking/generate_parentheses.cpp
giuseppe-coco 7cbfba3
Merge branch 'master' into master
giuseppe-coco 5fada63
updating DIRECTORY.md
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
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,97 @@ | ||
/** | ||
* @file | ||
* @brief Generates all combinations of well-formed parentheses. | ||
* | ||
* @details a sequence of parentheses is well-formed if each opening parentheses | ||
has a corresponding closing parenthesis | ||
* and the closing parentheses are correctly ordered | ||
* | ||
* @author [Giuseppe Coco](https://github.com/WoWS17) | ||
|
||
*/ | ||
|
||
#include <cassert> | ||
#include <iostream> | ||
#include <vector> | ||
giuseppe-coco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class generate_parentheses { | ||
Panquesito7 marked this conversation as resolved.
Show resolved
Hide resolved
Panquesito7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private: | ||
std::vector<std::string> res; // Contains all possible valid patterns | ||
giuseppe-coco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @brief function that implements backtracking | ||
* | ||
* @param str string build during backtracking | ||
* @param n number of pairs of parentheses | ||
* @param closed number of closed parentheses | ||
* @param open number of open parentheses | ||
*/ | ||
|
||
void makeStrings(std::string str, int n, int closed, int open) { | ||
giuseppe-coco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (closed > open) // We can never have more closed than open | ||
return; | ||
|
||
if (str.length() == 2 * n and | ||
closed != open) // closed and open must be the same | ||
return; | ||
|
||
if (str.length() == 2 * n) { | ||
res.push_back(str); | ||
return; | ||
} | ||
|
||
makeStrings(str + ')', n, closed + 1, open); | ||
makeStrings(str + '(', n, closed, open + 1); | ||
} | ||
|
||
public: | ||
/** | ||
* @brief wrapper interface | ||
* | ||
* @param n number of pairs of parentheses | ||
* @return all well-formed pattern of parentheses | ||
*/ | ||
std::vector<std::string> generate_parenthesis(int n) { | ||
res.clear(); | ||
std::string str = "("; | ||
makeStrings(str, n, 0, 1); | ||
return res; | ||
} | ||
}; | ||
Panquesito7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() { | ||
int n; | ||
giuseppe-coco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::vector<std::string> patterns; | ||
generate_parentheses p; | ||
giuseppe-coco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
n = 1; | ||
patterns = {{"()"}}; | ||
assert(p.generate_parenthesis(n) == patterns); | ||
|
||
n = 3; | ||
patterns = {{"()()()"}, {"()(())"}, {"(())()"}, {"(()())"}, {"((()))"}}; | ||
|
||
assert(p.generate_parenthesis(n) == patterns); | ||
|
||
n = 4; | ||
patterns = {{"()()()()"}, {"()()(())"}, {"()(())()"}, {"()(()())"}, | ||
{"()((()))"}, {"(())()()"}, {"(())(())"}, {"(()())()"}, | ||
{"(()()())"}, {"(()(()))"}, {"((()))()"}, {"((())())"}, | ||
{"((()()))"}, {"(((())))"}}; | ||
assert(p.generate_parenthesis(n) == patterns); | ||
|
||
std::cout << "All tests passed\n"; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); | ||
Panquesito7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 0; | ||
} | ||
giuseppe-coco marked this conversation as resolved.
Show resolved
Hide resolved
|
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.