Merge branch 'master' into longest_common_string_patch

This commit is contained in:
realstealthninja 2023-06-01 06:32:07 +05:30 committed by GitHub
commit 8ca74d2cb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 205 additions and 82 deletions

1
.github/CODEOWNERS vendored Normal file
View File

@ -0,0 +1 @@
* @Panquesito7 @tjgurwara99 @realstealthninja

View File

@ -94,6 +94,8 @@ jobs:
name: Compile checks name: Compile checks
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
needs: [MainSequence] needs: [MainSequence]
permissions:
pull-requests: write
strategy: strategy:
matrix: matrix:
os: [ubuntu-latest, windows-latest, macOS-latest] os: [ubuntu-latest, windows-latest, macOS-latest]
@ -101,5 +103,17 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
with: with:
submodules: true submodules: true
- run: cmake -B ./build -S . - run: |
- run: cmake --build build cmake -B ./build -S .
cmake --build build
- name: Label on PR fail
uses: actions/github-script@v6
if: ${{ failure() && matrix.os == 'ubuntu-latest' && github.event_name == 'pull_request' }}
with:
script: |
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['automated tests are failing']
})

View File

@ -1,5 +1,6 @@
## Backtracking ## Backtracking
* [Generate Parentheses](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/generate_parentheses.cpp)
* [Graph Coloring](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/graph_coloring.cpp) * [Graph Coloring](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/graph_coloring.cpp)
* [Knight Tour](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/knight_tour.cpp) * [Knight Tour](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/knight_tour.cpp)
* [Magic Sequence](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/magic_sequence.cpp) * [Magic Sequence](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/backtracking/magic_sequence.cpp)

View File

@ -0,0 +1,113 @@
/**
* @file
* @brief Well-formed [Generated
* Parentheses](https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/794/) with all combinations.
*
* @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> /// for assert
#include <iostream> /// for I/O operation
#include <vector> /// for vector container
/**
* @brief Backtracking algorithms
* @namespace backtracking
*/
namespace backtracking {
/**
* @brief generate_parentheses class
*/
class generate_parentheses {
private:
std::vector<std::string> res; ///< Contains all possible valid patterns
void makeStrings(std::string str, int n, int closed, int open);
public:
std::vector<std::string> generate(int n);
};
/**
* @brief function that adds parenthesis to the string.
*
* @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 generate_parentheses::makeStrings(std::string str, int n,
int closed, int open) {
if (closed > open) // We can never have more closed than open
return;
if ((str.length() == 2 * n) &&
(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);
}
/**
* @brief wrapper interface
*
* @param n number of pairs of parentheses
* @return all well-formed pattern of parentheses
*/
std::vector<std::string> generate_parentheses::generate(int n) {
backtracking::generate_parentheses::res.clear();
std::string str = "(";
generate_parentheses::makeStrings(str, n, 0, 1);
return res;
}
} // namespace backtracking
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
int n = 0;
std::vector<std::string> patterns;
backtracking::generate_parentheses p;
n = 1;
patterns = {{"()"}};
assert(p.generate(n) == patterns);
n = 3;
patterns = {{"()()()"}, {"()(())"}, {"(())()"}, {"(()())"}, {"((()))"}};
assert(p.generate(n) == patterns);
n = 4;
patterns = {{"()()()()"}, {"()()(())"}, {"()(())()"}, {"()(()())"},
{"()((()))"}, {"(())()()"}, {"(())(())"}, {"(()())()"},
{"(()()())"}, {"(()(()))"}, {"((()))()"}, {"((())())"},
{"((()()))"}, {"(((())))"}};
assert(p.generate(n) == patterns);
std::cout << "All tests passed\n";
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}

View File

@ -1,7 +1,7 @@
/** /**
* @file * @file
* @brief Program to find the Longest Palindormic * @brief Program to find the [Longest Palindormic
* Subsequence of a string * Subsequence](https://www.geeksforgeeks.org/longest-palindromic-subsequence-dp-12/) of a string
* *
* @details * @details
* [Palindrome](https://en.wikipedia.org/wiki/Palindrome) string sequence of * [Palindrome](https://en.wikipedia.org/wiki/Palindrome) string sequence of
@ -13,26 +13,33 @@
* @author [Anjali Jha](https://github.com/anjali1903) * @author [Anjali Jha](https://github.com/anjali1903)
*/ */
#include <algorithm> #include <cassert> /// for assert
#include <cassert> #include <string> /// for std::string
#include <iostream> #include <vector> /// for std::vector
#include <vector>
/** /**
* Function that returns the longest palindromic * @namespace
* subsequence of a string * @brief Dynamic Programming algorithms
*/ */
std::string lps(std::string a) { namespace dynamic_programming {
std::string b = a; /**
reverse(b.begin(), b.end()); * @brief Function that returns the longest palindromic
int m = a.length(); * subsequence of a string
std::vector<std::vector<int> > res(m + 1); * @param a string whose longest palindromic subsequence is to be found
* @returns longest palindromic subsequence of the string
*/
std::string lps(const std::string& a) {
const auto b = std::string(a.rbegin(), a.rend());
const auto m = a.length();
using ind_type = std::string::size_type;
std::vector<std::vector<ind_type> > res(m + 1,
std::vector<ind_type>(m + 1));
// Finding the length of the longest // Finding the length of the longest
// palindromic subsequence and storing // palindromic subsequence and storing
// in a 2D array in bottoms-up manner // in a 2D array in bottoms-up manner
for (int i = 0; i <= m; i++) { for (ind_type i = 0; i <= m; i++) {
for (int j = 0; j <= m; j++) { for (ind_type j = 0; j <= m; j++) {
if (i == 0 || j == 0) { if (i == 0 || j == 0) {
res[i][j] = 0; res[i][j] = 0;
} else if (a[i - 1] == b[j - 1]) { } else if (a[i - 1] == b[j - 1]) {
@ -43,10 +50,10 @@ std::string lps(std::string a) {
} }
} }
// Length of longest palindromic subsequence // Length of longest palindromic subsequence
int idx = res[m][m]; auto idx = res[m][m];
// Creating string of index+1 length // Creating string of index+1 length
std::string ans(idx + 1, '\0'); std::string ans(idx, '\0');
int i = m, j = m; ind_type i = m, j = m;
// starting from right-most bottom-most corner // starting from right-most bottom-most corner
// and storing them one by one in ans // and storing them one by one in ans
@ -70,19 +77,22 @@ std::string lps(std::string a) {
return ans; return ans;
} }
} // namespace dynamic_programming
/** Test function */ /**
void test() { * @brief Self-test implementations
// lps("radar") return "radar" * @returns void
assert(lps("radar") == "radar"); */
// lps("abbcbaa") return "abcba" static void test() {
assert(lps("abbcbaa") == "abcba"); assert(dynamic_programming::lps("radar") == "radar");
// lps("bbbab") return "bbbb" assert(dynamic_programming::lps("abbcbaa") == "abcba");
assert(lps("bbbab") == "bbbb"); assert(dynamic_programming::lps("bbbab") == "bbbb");
assert(dynamic_programming::lps("") == "");
} }
/** /**
* Main Function * @brief Main Function
* @returns 0 on exit
*/ */
int main() { int main() {
test(); // execute the tests test(); // execute the tests

View File

@ -15,10 +15,9 @@
* @author [Swastika Gupta](https://github.com/Swastyy) * @author [Swastika Gupta](https://github.com/Swastyy)
*/ */
#include <algorithm> /// for std::is_equal, std::swap #include <cassert> /// for assert
#include <cassert> /// for assert #include <iostream> /// for std::cout
#include <iostream> /// for IO operations #include <vector> /// for std::vector
#include <vector> /// for std::vector
/** /**
* @namespace math * @namespace math
@ -39,10 +38,17 @@ namespace n_bonacci {
* @returns the n-bonacci sequence as vector array * @returns the n-bonacci sequence as vector array
*/ */
std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) { std::vector<uint64_t> N_bonacci(const uint64_t &n, const uint64_t &m) {
std::vector<uint64_t> a(m, 0); // we create an empty array of size m std::vector<uint64_t> a(
m, 0); // we create an array of size m filled with zeros
if (m < n || n == 0) {
return a;
}
a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of a[n - 1] = 1; /// we initialise the (n-1)th term as 1 which is the sum of
/// preceding N zeros /// preceding N zeros
if (n == m) {
return a;
}
a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is a[n] = 1; /// similarily the sum of preceding N zeros and the (N+1)th 1 is
/// also 1 /// also 1
for (uint64_t i = n + 1; i < m; i++) { 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) {
* @returns void * @returns void
*/ */
static void test() { static void test() {
// n = 1 m = 1 return [1, 1] struct TestCase {
std::cout << "1st test"; const uint64_t n;
std::vector<uint64_t> arr1 = math::n_bonacci::N_bonacci( const uint64_t m;
1, 1); // first input is the param n and second one is the param m for const std::vector<uint64_t> expected;
// N-bonacci func TestCase(const uint64_t in_n, const uint64_t in_m,
std::vector<uint64_t> output_array1 = { std::initializer_list<uint64_t> data)
1, 1}; // It is the expected output series of length m : n(in_n), m(in_m), expected(data) {
assert(std::equal(std::begin(arr1), std::end(arr1), assert(data.size() == m);
std::begin(output_array1))); }
std::cout << "passed" << std::endl; };
const std::vector<TestCase> test_cases = {
TestCase(0, 0, {}),
TestCase(0, 1, {0}),
TestCase(0, 2, {0, 0}),
TestCase(1, 0, {}),
TestCase(1, 1, {1}),
TestCase(1, 2, {1, 1}),
TestCase(1, 3, {1, 1, 1}),
TestCase(5, 15, {0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, 464}),
TestCase(
6, 17,
{0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248, 492, 976}),
TestCase(56, 15, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})};
// n = 5 m = 15 return [0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 31, 61, 120, 236, for (const auto &tc : test_cases) {
// 464] assert(math::n_bonacci::N_bonacci(tc.n, tc.m) == tc.expected);
std::cout << "2nd test"; }
std::vector<uint64_t> arr2 = math::n_bonacci::N_bonacci(
5, 15); // first input is the param n and second one is the param m for
// N-bonacci func
std::vector<uint64_t> output_array2 = {
0, 0, 0, 0, 1, 1, 2, 4,
8, 16, 31, 61, 120, 236, 464}; // It is the expected output series of
// length m
assert(std::equal(std::begin(arr2), std::end(arr2),
std::begin(output_array2)));
std::cout << "passed" << std::endl;
// n = 6 m = 17 return [0, 0, 0, 0, 0, 1, 1, 2, 4, 8, 16, 32, 63, 125, 248,
// 492, 976]
std::cout << "3rd test";
std::vector<uint64_t> arr3 = math::n_bonacci::N_bonacci(
6, 17); // first input is the param n and second one is the param m for
// N-bonacci func
std::vector<uint64_t> output_array3 = {
0, 0, 0, 0, 0, 1, 1, 2, 4,
8, 16, 32, 63, 125, 248, 492, 976}; // It is the expected output series
// of length m
assert(std::equal(std::begin(arr3), std::end(arr3),
std::begin(output_array3)));
std::cout << "passed" << std::endl;
// n = 56 m = 15 return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
std::cout << "4th test";
std::vector<uint64_t> arr4 = math::n_bonacci::N_bonacci(
56, 15); // first input is the param n and second one is the param m
// for N-bonacci func
std::vector<uint64_t> output_array4 = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0}; // It is the expected output series of length m
assert(std::equal(std::begin(arr4), std::end(arr4),
std::begin(output_array4)));
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
} }