Skip to content

Commit 651432c

Browse files
Merge branch 'master' into factorial-iterative
2 parents dcd4c81 + 0934ec4 commit 651432c

File tree

4 files changed

+168
-28
lines changed

4 files changed

+168
-28
lines changed

.github/workflows/awesome_workflow.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,26 @@ jobs:
9494
name: Compile checks
9595
runs-on: ${{ matrix.os }}
9696
needs: [MainSequence]
97+
permissions:
98+
pull-requests: write
9799
strategy:
98100
matrix:
99101
os: [ubuntu-latest, windows-latest, macOS-latest]
100102
steps:
101103
- uses: actions/checkout@v3
102104
with:
103105
submodules: true
104-
- run: cmake -B ./build -S .
105-
- run: cmake --build build
106+
- run: |
107+
cmake -B ./build -S .
108+
cmake --build build
109+
- name: Label on PR fail
110+
uses: actions/github-script@v6
111+
if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }}
112+
with:
113+
script: |
114+
github.rest.issues.addLabels({
115+
issue_number: context.issue.number,
116+
owner: context.repo.owner,
117+
repo: context.repo.repo,
118+
labels: ['automated tests are failing']
119+
})

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
## Backtracking
3+
* [Generate Parentheses](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/generate_parentheses.cpp)
34
* [Graph Coloring](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/graph_coloring.cpp)
45
* [Knight Tour](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/knight_tour.cpp)
56
* [Magic Sequence](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/magic_sequence.cpp)
@@ -67,6 +68,7 @@
6768
* [Queue Using Two Stacks](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/queue_using_two_stacks.cpp)
6869
* [Rb Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/rb_tree.cpp)
6970
* [Reverse A Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/reverse_a_linked_list.cpp)
71+
* [Segment Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/segment_tree.cpp)
7072
* [Skip List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/skip_list.cpp)
7173
* [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/sparse_table.cpp)
7274
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.hpp)
@@ -228,6 +230,7 @@
228230
* [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/prime_factorization.cpp)
229231
* [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/prime_numbers.cpp)
230232
* [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/primes_up_to_billion.cpp)
233+
* [Quadratic Equations Complex Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/quadratic_equations_complex_numbers.cpp)
231234
* [Realtime Stats](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/realtime_stats.cpp)
232235
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/sieve_of_eratosthenes.cpp)
233236
* [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/sqrt_double.cpp)

backtracking/generate_parentheses.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @file
3+
* @brief Well-formed [Generated
4+
* Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) with all combinations.
5+
*
6+
* @details a sequence of parentheses is well-formed if each opening parentheses
7+
* has a corresponding closing parenthesis
8+
* and the closing parentheses are correctly ordered
9+
*
10+
* @author [Giuseppe Coco](https://github.com/WoWS17)
11+
12+
*/
13+
14+
#include <cassert> /// for assert
15+
#include <iostream> /// for I/O operation
16+
#include <vector> /// for vector container
17+
18+
/**
19+
* @brief Backtracking algorithms
20+
* @namespace backtracking
21+
*/
22+
namespace backtracking {
23+
/**
24+
* @brief generate_parentheses class
25+
*/
26+
class generate_parentheses {
27+
private:
28+
std::vector<std::string> res; ///< Contains all possible valid patterns
29+
30+
void makeStrings(std::string str, int n, int closed, int open);
31+
32+
public:
33+
std::vector<std::string> generate(int n);
34+
};
35+
36+
/**
37+
* @brief function that adds parenthesis to the string.
38+
*
39+
* @param str string build during backtracking
40+
* @param n number of pairs of parentheses
41+
* @param closed number of closed parentheses
42+
* @param open number of open parentheses
43+
*/
44+
45+
void generate_parentheses::makeStrings(std::string str, int n,
46+
int closed, int open) {
47+
if (closed > open) // We can never have more closed than open
48+
return;
49+
50+
if ((str.length() == 2 * n) &&
51+
(closed != open)) { // closed and open must be the same
52+
return;
53+
}
54+
55+
if (str.length() == 2 * n) {
56+
res.push_back(str);
57+
return;
58+
}
59+
60+
makeStrings(str + ')', n, closed + 1, open);
61+
makeStrings(str + '(', n, closed, open + 1);
62+
}
63+
64+
/**
65+
* @brief wrapper interface
66+
*
67+
* @param n number of pairs of parentheses
68+
* @return all well-formed pattern of parentheses
69+
*/
70+
std::vector<std::string> generate_parentheses::generate(int n) {
71+
backtracking::generate_parentheses::res.clear();
72+
std::string str = "(";
73+
generate_parentheses::makeStrings(str, n, 0, 1);
74+
return res;
75+
}
76+
} // namespace backtracking
77+
78+
/**
79+
* @brief Self-test implementations
80+
* @returns void
81+
*/
82+
static void test() {
83+
int n = 0;
84+
std::vector<std::string> patterns;
85+
backtracking::generate_parentheses p;
86+
87+
n = 1;
88+
patterns = {{"()"}};
89+
assert(p.generate(n) == patterns);
90+
91+
n = 3;
92+
patterns = {{"()()()"}, {"()(())"}, {"(())()"}, {"(()())"}, {"((()))"}};
93+
94+
assert(p.generate(n) == patterns);
95+
96+
n = 4;
97+
patterns = {{"()()()()"}, {"()()(())"}, {"()(())()"}, {"()(()())"},
98+
{"()((()))"}, {"(())()()"}, {"(())(())"}, {"(()())()"},
99+
{"(()()())"}, {"(()(()))"}, {"((()))()"}, {"((())())"},
100+
{"((()()))"}, {"(((())))"}};
101+
assert(p.generate(n) == patterns);
102+
103+
std::cout << "All tests passed\n";
104+
}
105+
106+
/**
107+
* @brief Main function
108+
* @returns 0 on exit
109+
*/
110+
int main() {
111+
test(); // run self-test implementations
112+
return 0;
113+
}

dynamic_programming/longest_palindromic_subsequence.cpp

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file
3-
* @brief Program to find the Longest Palindormic
4-
* Subsequence of a string
3+
* @brief Program to find the [Longest Palindormic
4+
* Subsequence](https://www.geeksforgeeks.org/longest-palindromic-subsequence-dp-12/) of a string
55
*
66
* @details
77
* [Palindrome](https://en.wikipedia.org/wiki/Palindrome) string sequence of
@@ -13,26 +13,33 @@
1313
* @author [Anjali Jha](https://github.com/anjali1903)
1414
*/
1515

16-
#include <algorithm>
17-
#include <cassert>
18-
#include <iostream>
19-
#include <vector>
16+
#include <cassert> /// for assert
17+
#include <string> /// for std::string
18+
#include <vector> /// for std::vector
2019

2120
/**
22-
* Function that returns the longest palindromic
21+
* @namespace
22+
* @brief Dynamic Programming algorithms
23+
*/
24+
namespace dynamic_programming {
25+
/**
26+
* @brief Function that returns the longest palindromic
2327
* subsequence of a string
28+
* @param a string whose longest palindromic subsequence is to be found
29+
* @returns longest palindromic subsequence of the string
2430
*/
25-
std::string lps(std::string a) {
26-
std::string b = a;
27-
reverse(b.begin(), b.end());
28-
int m = a.length();
29-
std::vector<std::vector<int> > res(m + 1);
31+
std::string lps(const std::string& a) {
32+
const auto b = std::string(a.rbegin(), a.rend());
33+
const auto m = a.length();
34+
using ind_type = std::string::size_type;
35+
std::vector<std::vector<ind_type> > res(m + 1,
36+
std::vector<ind_type>(m + 1));
3037

3138
// Finding the length of the longest
3239
// palindromic subsequence and storing
3340
// in a 2D array in bottoms-up manner
34-
for (int i = 0; i <= m; i++) {
35-
for (int j = 0; j <= m; j++) {
41+
for (ind_type i = 0; i <= m; i++) {
42+
for (ind_type j = 0; j <= m; j++) {
3643
if (i == 0 || j == 0) {
3744
res[i][j] = 0;
3845
} else if (a[i - 1] == b[j - 1]) {
@@ -43,10 +50,10 @@ std::string lps(std::string a) {
4350
}
4451
}
4552
// Length of longest palindromic subsequence
46-
int idx = res[m][m];
53+
auto idx = res[m][m];
4754
// Creating string of index+1 length
48-
std::string ans(idx + 1, '\0');
49-
int i = m, j = m;
55+
std::string ans(idx, '\0');
56+
ind_type i = m, j = m;
5057

5158
// starting from right-most bottom-most corner
5259
// and storing them one by one in ans
@@ -70,19 +77,22 @@ std::string lps(std::string a) {
7077

7178
return ans;
7279
}
80+
} // namespace dynamic_programming
7381

74-
/** Test function */
75-
void test() {
76-
// lps("radar") return "radar"
77-
assert(lps("radar") == "radar");
78-
// lps("abbcbaa") return "abcba"
79-
assert(lps("abbcbaa") == "abcba");
80-
// lps("bbbab") return "bbbb"
81-
assert(lps("bbbab") == "bbbb");
82+
/**
83+
* @brief Self-test implementations
84+
* @returns void
85+
*/
86+
static void test() {
87+
assert(dynamic_programming::lps("radar") == "radar");
88+
assert(dynamic_programming::lps("abbcbaa") == "abcba");
89+
assert(dynamic_programming::lps("bbbab") == "bbbb");
90+
assert(dynamic_programming::lps("") == "");
8291
}
8392

8493
/**
85-
* Main Function
94+
* @brief Main Function
95+
* @returns 0 on exit
8696
*/
8797
int main() {
8898
test(); // execute the tests

0 commit comments

Comments
 (0)