Skip to content

Commit 8ca74d2

Browse files
Merge branch 'master' into longest_common_string_patch
2 parents da25674 + ea4100e commit 8ca74d2

File tree

6 files changed

+204
-81
lines changed

6 files changed

+204
-81
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @Panquesito7 @tjgurwara99 @realstealthninja

.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: 1 addition & 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)
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

math/n_bonacci.cpp

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
* @author [Swastika Gupta](https://github.com/Swastyy)
1616
*/
1717

18-
#include <algorithm> /// for std::is_equal, std::swap
19-
#include <cassert> /// for assert
20-
#include <iostream> /// for IO operations
21-
#include <vector> /// for std::vector
18+
#include <cassert> /// for assert
19+
#include <iostream> /// for std::cout
20+
#include <vector> /// for std::vector
2221

2322
/**
2423
* @namespace math
@@ -39,10 +38,17 @@ namespace n_bonacci {
3938
* @returns the n-bonacci sequence as vector array
4039
*/
4140
std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) {
42-
std::vector<uint64_t> a(m, 0); // we create an empty array of size m
41+
std::vector<uint64_t> a(
42+
m, 0); // we create an array of size m filled with zeros
43+
if (m < n || n == 0) {
44+
return a;
45+
}
4346

4447
a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of
4548
/// preceding N zeros
49+
if (n == m) {
50+
return a;
51+
}
4652
a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is
4753
/// also 1
4854
for (uint64_t i = n + 1; i < m; i++) {
@@ -61,55 +67,33 @@ std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) {
6167
* @returns void
6268
*/
6369
static void test() {
64-
// n = 1 m = 1 return [1, 1]
65-
std::cout << "1st test";
66-
std::vector<uint64_t> arr1 = math::n_bonacci::N_bonacci(
67-
1, 1); // first input is the param n and second one is the param m for
68-
// N-bonacci func
69-
std::vector<uint64_t> output_array1 = {
70-
1, 1}; // It is the expected output series of length m
71-
assert(std::equal(std::begin(arr1), std::end(arr1),
72-
std::begin(output_array1)));
73-
std::cout << "passed" << std::endl;
74-
75-
// n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236,
76-
// 464]
77-
std::cout << "2nd test";
78-
std::vector<uint64_t> arr2 = math::n_bonacci::N_bonacci(
79-
5, 15); // first input is the param n and second one is the param m for
80-
// N-bonacci func
81-
std::vector<uint64_t> output_array2 = {
82-
0, 0, 0, 0, 1, 1, 2, 4,
83-
8, 16, 31, 61, 120, 236, 464}; // It is the expected output series of
84-
// length m
85-
assert(std::equal(std::begin(arr2), std::end(arr2),
86-
std::begin(output_array2)));
87-
std::cout << "passed" << std::endl;
88-
89-
// n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248,
90-
// 492, 976]
91-
std::cout << "3rd test";
92-
std::vector<uint64_t> arr3 = math::n_bonacci::N_bonacci(
93-
6, 17); // first input is the param n and second one is the param m for
94-
// N-bonacci func
95-
std::vector<uint64_t> output_array3 = {
96-
0, 0, 0, 0, 0, 1, 1, 2, 4,
97-
8, 16, 32, 63, 125, 248, 492, 976}; // It is the expected output series
98-
// of length m
99-
assert(std::equal(std::begin(arr3), std::end(arr3),
100-
std::begin(output_array3)));
101-
std::cout << "passed" << std::endl;
70+
struct TestCase {
71+
const uint64_t n;
72+
const uint64_t m;
73+
const std::vector<uint64_t> expected;
74+
TestCase(const uint64_t in_n, const uint64_t in_m,
75+
std::initializer_list<uint64_t> data)
76+
: n(in_n), m(in_m), expected(data) {
77+
assert(data.size() == m);
78+
}
79+
};
80+
const std::vector<TestCase> test_cases = {
81+
TestCase(0, 0, {}),
82+
TestCase(0, 1, {0}),
83+
TestCase(0, 2, {0, 0}),
84+
TestCase(1, 0, {}),
85+
TestCase(1, 1, {1}),
86+
TestCase(1, 2, {1, 1}),
87+
TestCase(1, 3, {1, 1, 1}),
88+
TestCase(5, 15, {0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464}),
89+
TestCase(
90+
6, 17,
91+
{0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976}),
92+
TestCase(56, 15, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})};
10293

103-
// n = 56 m = 15 return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
104-
std::cout << "4th test";
105-
std::vector<uint64_t> arr4 = math::n_bonacci::N_bonacci(
106-
56, 15); // first input is the param n and second one is the param m
107-
// for N-bonacci func
108-
std::vector<uint64_t> output_array4 = {
109-
0, 0, 0, 0, 0, 0, 0, 0,
110-
0, 0, 0, 0, 0, 0, 0}; // It is the expected output series of length m
111-
assert(std::equal(std::begin(arr4), std::end(arr4),
112-
std::begin(output_array4)));
94+
for (const auto &tc : test_cases) {
95+
assert(math::n_bonacci::N_bonacci(tc.n, tc.m) == tc.expected);
96+
}
11397
std::cout << "passed" << std::endl;
11498
}
11599

0 commit comments

Comments
 (0)