mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-09 05:27:11 +08:00
Merge branch 'master' into intersection-of-two-arrays
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/hamming_distance.cpp)
|
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/bit_manipulation/hamming_distance.cpp)
|
||||||
|
|
||||||
## Ciphers
|
## Ciphers
|
||||||
|
* [A1Z26 Cipher](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/ciphers/a1z26_cipher.cpp)
|
||||||
* [Atbash Cipher](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/ciphers/atbash_cipher.cpp)
|
* [Atbash Cipher](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/ciphers/atbash_cipher.cpp)
|
||||||
* [Base64 Encoding](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/ciphers/base64_encoding.cpp)
|
* [Base64 Encoding](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/ciphers/base64_encoding.cpp)
|
||||||
* [Caesar Cipher](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/ciphers/caesar_cipher.cpp)
|
* [Caesar Cipher](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/ciphers/caesar_cipher.cpp)
|
||||||
@@ -245,7 +246,7 @@
|
|||||||
* [Reverse Binary Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/reverse_binary_tree.cpp)
|
* [Reverse Binary Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/reverse_binary_tree.cpp)
|
||||||
* [Selectionsortlinkedlist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/selectionsortlinkedlist.cpp)
|
* [Selectionsortlinkedlist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/selectionsortlinkedlist.cpp)
|
||||||
* [Trie Multiple Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/trie_multiple_search.cpp)
|
* [Trie Multiple Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/trie_multiple_search.cpp)
|
||||||
* [Union Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/union_of_2_arrays.cpp)
|
* [Union Of Two Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/union_of_two_arrays.cpp)
|
||||||
|
|
||||||
## Others
|
## Others
|
||||||
* [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/buzz_number.cpp)
|
* [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/buzz_number.cpp)
|
||||||
|
|||||||
162
ciphers/a1z26_cipher.cpp
Normal file
162
ciphers/a1z26_cipher.cpp
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* @brief Implementation of the [A1Z26
|
||||||
|
* cipher](https://www.dcode.fr/letter-number-cipher)
|
||||||
|
* @details The A1Z26 cipher is a simple substiution cipher where each letter is
|
||||||
|
* replaced by the number of the order they're in. For example, A corresponds to
|
||||||
|
* 1, B = 2, C = 3, etc.
|
||||||
|
*
|
||||||
|
* @author [Focusucof](https://github.com/Focusucof)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <algorithm> /// for std::transform and std::replace
|
||||||
|
#include <cassert> /// for assert
|
||||||
|
#include <cstdint> /// for uint8_t
|
||||||
|
#include <iostream> /// for IO operations
|
||||||
|
#include <map> /// for std::map
|
||||||
|
#include <sstream> /// for std::stringstream
|
||||||
|
#include <string> /// for std::string
|
||||||
|
#include <vector> /// for std::vector
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @namespace ciphers
|
||||||
|
* @brief Algorithms for encryption and decryption
|
||||||
|
*/
|
||||||
|
namespace ciphers {
|
||||||
|
/**
|
||||||
|
* @namespace a1z26
|
||||||
|
* @brief Functions for [A1Z26](https://www.dcode.fr/letter-number-cipher)
|
||||||
|
* encryption and decryption implementation
|
||||||
|
*/
|
||||||
|
namespace a1z26 {
|
||||||
|
|
||||||
|
std::map<uint8_t, char> a1z26_decrypt_map = {
|
||||||
|
{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}, {5, 'e'}, {6, 'f'}, {7, 'g'},
|
||||||
|
{8, 'h'}, {9, 'i'}, {10, 'j'}, {11, 'k'}, {12, 'l'}, {13, 'm'}, {14, 'n'},
|
||||||
|
{15, 'o'}, {16, 'p'}, {17, 'q'}, {18, 'r'}, {19, 's'}, {20, 't'}, {21, 'u'},
|
||||||
|
{22, 'v'}, {23, 'w'}, {24, 'x'}, {25, 'y'}, {26, 'z'},
|
||||||
|
};
|
||||||
|
|
||||||
|
std::map<char, uint8_t> a1z26_encrypt_map = {
|
||||||
|
{'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5}, {'f', 6}, {'g', 7},
|
||||||
|
{'h', 8}, {'i', 9}, {'j', 10}, {'k', 11}, {'l', 12}, {'m', 13}, {'n', 14},
|
||||||
|
{'o', 15}, {'p', 16}, {'q', 17}, {'r', 18}, {'s', 19}, {'t', 20}, {'u', 21},
|
||||||
|
{'v', 22}, {'w', 23}, {'x', 24}, {'y', 25}, {'z', 26}};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief a1z26 encryption implementation
|
||||||
|
* @param text is the plaintext input
|
||||||
|
* @returns encoded string with dashes to seperate letters
|
||||||
|
*/
|
||||||
|
std::string encrypt(std::string text) {
|
||||||
|
std::string result;
|
||||||
|
std::transform(text.begin(), text.end(), text.begin(),
|
||||||
|
::tolower); // convert string to lowercase
|
||||||
|
std::replace(text.begin(), text.end(), ':', ' ');
|
||||||
|
for (char letter : text) {
|
||||||
|
if (letter != ' ') {
|
||||||
|
result += std::to_string(
|
||||||
|
a1z26_encrypt_map[letter]); // convert int to string and append
|
||||||
|
// to result
|
||||||
|
result += "-"; // space out each set of numbers with spaces
|
||||||
|
} else {
|
||||||
|
result.pop_back();
|
||||||
|
result += ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.pop_back(); // remove leading dash
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief a1z26 decryption implementation
|
||||||
|
* @param text is the encrypted text input
|
||||||
|
* @param bReturnUppercase is if the decoded string should be in uppercase or
|
||||||
|
* not
|
||||||
|
* @returns the decrypted string in all uppercase or all lowercase
|
||||||
|
*/
|
||||||
|
std::string decrypt(const std::string& text, bool bReturnUppercase = false) {
|
||||||
|
std::string result;
|
||||||
|
|
||||||
|
// split words seperated by spaces into a vector array
|
||||||
|
std::vector<std::string> word_array;
|
||||||
|
std::stringstream sstream(text);
|
||||||
|
std::string word;
|
||||||
|
while (sstream >> word) {
|
||||||
|
word_array.push_back(word);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& i : word_array) {
|
||||||
|
std::replace(i.begin(), i.end(), '-', ' ');
|
||||||
|
std::vector<std::string> text_array;
|
||||||
|
|
||||||
|
std::stringstream ss(i);
|
||||||
|
std::string res_text;
|
||||||
|
while (ss >> res_text) {
|
||||||
|
text_array.push_back(res_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& i : text_array) {
|
||||||
|
result += a1z26_decrypt_map[stoi(i)];
|
||||||
|
}
|
||||||
|
|
||||||
|
result += ' ';
|
||||||
|
}
|
||||||
|
result.pop_back(); // remove any leading whitespace
|
||||||
|
|
||||||
|
if (bReturnUppercase) {
|
||||||
|
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace a1z26
|
||||||
|
} // namespace ciphers
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Self-test implementations
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
static void test() {
|
||||||
|
// 1st test
|
||||||
|
std::string input = "Hello World";
|
||||||
|
std::string expected = "8-5-12-12-15 23-15-18-12-4";
|
||||||
|
std::string output = ciphers::a1z26::encrypt(input);
|
||||||
|
|
||||||
|
std::cout << "Input: " << input << std::endl;
|
||||||
|
std::cout << "Expected: " << expected << std::endl;
|
||||||
|
std::cout << "Output: " << output << std::endl;
|
||||||
|
assert(output == expected);
|
||||||
|
std::cout << "TEST PASSED";
|
||||||
|
|
||||||
|
// 2nd test
|
||||||
|
input = "12-15-23-5-18-3-1-19-5";
|
||||||
|
expected = "lowercase";
|
||||||
|
output = ciphers::a1z26::decrypt(input);
|
||||||
|
|
||||||
|
std::cout << "Input: " << input << std::endl;
|
||||||
|
std::cout << "Expected: " << expected << std::endl;
|
||||||
|
std::cout << "Output: " << output << std::endl;
|
||||||
|
assert(output == expected);
|
||||||
|
std::cout << "TEST PASSED";
|
||||||
|
|
||||||
|
// 3rd test
|
||||||
|
input = "21-16-16-5-18-3-1-19-5";
|
||||||
|
expected = "UPPERCASE";
|
||||||
|
output = ciphers::a1z26::decrypt(input, true);
|
||||||
|
|
||||||
|
std::cout << "Input: " << input << std::endl;
|
||||||
|
std::cout << "Expected: " << expected << std::endl;
|
||||||
|
std::cout << "Output: " << output << std::endl;
|
||||||
|
assert(output == expected);
|
||||||
|
std::cout << "TEST PASSED";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Main function
|
||||||
|
* @returns 0 on exit
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
test(); // run self-test implementations
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -7,12 +7,13 @@
|
|||||||
* @brief
|
* @brief
|
||||||
* Reduced all possibilities of a number which cannot be prime.
|
* Reduced all possibilities of a number which cannot be prime.
|
||||||
* Eg: No even number, except 2 can be a prime number, hence we will increment
|
* Eg: No even number, except 2 can be a prime number, hence we will increment
|
||||||
* our loop with i+2 jumping on all odd numbers only. If number is <= 1 or if it
|
* our loop with i+6 jumping and check for i or i+2 to be a factor of the number;
|
||||||
* is even except 2, break the loop and return false telling number is not
|
* if it's a factor then we will return false otherwise true after the loop terminates at the terminating condition which is (i*i<=num)
|
||||||
* prime.
|
|
||||||
*/
|
*/
|
||||||
#include <cassert>
|
|
||||||
#include <iostream>
|
#include <cassert> /// for assert
|
||||||
|
#include <iostream> /// for IO operations
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to check if the given number is prime or not.
|
* Function to check if the given number is prime or not.
|
||||||
* @param num number to be checked.
|
* @param num number to be checked.
|
||||||
@@ -23,14 +24,14 @@ bool is_prime(T num) {
|
|||||||
bool result = true;
|
bool result = true;
|
||||||
if (num <= 1) {
|
if (num <= 1) {
|
||||||
return false;
|
return false;
|
||||||
} else if (num == 2) {
|
} else if (num == 2 || num==3) {
|
||||||
return true;
|
return true;
|
||||||
} else if ((num & 1) == 0) {
|
} else if ((num%2) == 0 || num%3 == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (num >= 3) {
|
else {
|
||||||
for (T i = 3; (i * i) <= (num); i = (i + 2)) {
|
for (T i = 5; (i * i) <= (num); i = (i + 6)) {
|
||||||
if ((num % i) == 0) {
|
if ((num % i) == 0 || (num%(i+2)==0 )) {
|
||||||
result = false;
|
result = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
int main() {
|
|
||||||
int m, n, i = 0, j = 0;
|
|
||||||
cout << "Enter size of both arrays:";
|
|
||||||
cin >> m >> n;
|
|
||||||
int a[m];
|
|
||||||
int b[n];
|
|
||||||
cout << "Enter elements of array 1:";
|
|
||||||
for (i = 0; i < m; i++) cin >> a[i];
|
|
||||||
cout << "Enter elements of array 2:";
|
|
||||||
for (i = 0; i < n; i++) cin >> b[i];
|
|
||||||
i = 0;
|
|
||||||
j = 0;
|
|
||||||
while ((i < m) && (j < n)) {
|
|
||||||
if (a[i] < b[j])
|
|
||||||
cout << a[i++] << " ";
|
|
||||||
else if (a[i] > b[j])
|
|
||||||
cout << b[j++] << " ";
|
|
||||||
else {
|
|
||||||
cout << a[i++];
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (i < m) cout << a[i++] << " ";
|
|
||||||
while (j < n) cout << b[j++] << " ";
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
219
operations_on_datastructures/union_of_two_arrays.cpp
Normal file
219
operations_on_datastructures/union_of_two_arrays.cpp
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* @brief Implementation for the [Union of two sorted
|
||||||
|
* Arrays](https://en.wikipedia.org/wiki/Union_(set_theory))
|
||||||
|
* algorithm.
|
||||||
|
* @details The Union of two arrays is the collection of all the unique elements
|
||||||
|
* in the first array, combined with all of the unique elements of a second
|
||||||
|
* array. This implementation uses ordered arrays, and an algorithm to correctly
|
||||||
|
* order them and return the result as a new array (vector).
|
||||||
|
* @author [Alvin](https://github.com/polarvoid)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <algorithm> /// for std::sort
|
||||||
|
#include <cassert> /// for assert
|
||||||
|
#include <iostream> /// for IO operations
|
||||||
|
#include <vector> /// for std::vector
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @namespace operations_on_datastructures
|
||||||
|
* @brief Operations on Data Structures
|
||||||
|
*/
|
||||||
|
namespace operations_on_datastructures {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Prints the values of a vector sequentially, ending with a newline
|
||||||
|
* character.
|
||||||
|
* @param array Reference to the array to be printed
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
void print(const std::vector<int32_t> &array) {
|
||||||
|
for (int64_t i : array) {
|
||||||
|
std::cout << i << " "; /// Print each value in the array
|
||||||
|
}
|
||||||
|
std::cout << "\n"; /// Print newline
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Gets the union of two sorted arrays, and returns them in a
|
||||||
|
* vector.
|
||||||
|
* @details An algorithm is used that compares the elements of the two vectors,
|
||||||
|
* appending the one that has a lower value, and incrementing the index for that
|
||||||
|
* array. If one of the arrays reaches its end, all the elements of the other
|
||||||
|
* are appended to the resultant vector.
|
||||||
|
* @param first A std::vector of sorted integer values
|
||||||
|
* @param second A std::vector of sorted integer values
|
||||||
|
* @returns A std::vector of the union of the two arrays, in ascending order
|
||||||
|
*/
|
||||||
|
std::vector<int32_t> get_union(const std::vector<int32_t> &first,
|
||||||
|
const std::vector<int32_t> &second) {
|
||||||
|
std::vector<int32_t> res; ///< Vector to hold the union
|
||||||
|
size_t f_index = 0; ///< Index for the first array
|
||||||
|
size_t s_index = 0; ///< Index for the second array
|
||||||
|
size_t f_length = first.size(); ///< Length of first array
|
||||||
|
size_t s_length = second.size(); ///< Length of second array
|
||||||
|
int32_t next = 0; ///< Integer to store value of the next element
|
||||||
|
|
||||||
|
while (f_index < f_length && s_index < s_length) {
|
||||||
|
if (first[f_index] < second[s_index]) {
|
||||||
|
next = first[f_index]; ///< Append from first array
|
||||||
|
f_index++; ///< Increment index of second array
|
||||||
|
} else if (first[f_index] > second[s_index]) {
|
||||||
|
next = second[s_index]; ///< Append from second array
|
||||||
|
s_index++; ///< Increment index of second array
|
||||||
|
} else {
|
||||||
|
next = first[f_index]; ///< Element is the same in both
|
||||||
|
f_index++; ///< Increment index of first array
|
||||||
|
s_index++; ///< Increment index of second array too
|
||||||
|
}
|
||||||
|
if ((res.size() == 0) || (next != res.back())) {
|
||||||
|
res.push_back(next); ///< Add the element if it is unique
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (f_index < f_length) {
|
||||||
|
next = first[f_index]; ///< Add remaining elements
|
||||||
|
if ((res.size() == 0) || (next != res.back())) {
|
||||||
|
res.push_back(next); ///< Add the element if it is unique
|
||||||
|
}
|
||||||
|
f_index++;
|
||||||
|
}
|
||||||
|
while (s_index < s_length) {
|
||||||
|
next = second[s_index]; ///< Add remaining elements
|
||||||
|
if ((res.size() == 0) || (next != res.back())) {
|
||||||
|
res.push_back(next); ///< Add the element if it is unique
|
||||||
|
}
|
||||||
|
s_index++;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace operations_on_datastructures
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @namespace tests
|
||||||
|
* @brief Testcases to check Union of Two Arrays.
|
||||||
|
*/
|
||||||
|
namespace tests {
|
||||||
|
using operations_on_datastructures::get_union;
|
||||||
|
using operations_on_datastructures::print;
|
||||||
|
/**
|
||||||
|
* @brief A Test to check an edge case (two empty arrays)
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
void test1() {
|
||||||
|
std::cout << "TEST CASE 1\n";
|
||||||
|
std::cout << "Intialized a = {} b = {}\n";
|
||||||
|
std::cout << "Expected result: {}\n";
|
||||||
|
std::vector<int32_t> a = {};
|
||||||
|
std::vector<int32_t> b = {};
|
||||||
|
std::vector<int32_t> result = get_union(a, b);
|
||||||
|
assert(result == a); ///< Check if result is empty
|
||||||
|
print(result); ///< Should only print newline
|
||||||
|
std::cout << "TEST PASSED!\n\n";
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @brief A Test to check an edge case (one empty array)
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
void test2() {
|
||||||
|
std::cout << "TEST CASE 2\n";
|
||||||
|
std::cout << "Intialized a = {} b = {2, 3}\n";
|
||||||
|
std::cout << "Expected result: {2, 3}\n";
|
||||||
|
std::vector<int32_t> a = {};
|
||||||
|
std::vector<int32_t> b = {2, 3};
|
||||||
|
std::vector<int32_t> result = get_union(a, b);
|
||||||
|
assert(result == b); ///< Check if result is equal to b
|
||||||
|
print(result); ///< Should print 2 3
|
||||||
|
std::cout << "TEST PASSED!\n\n";
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @brief A Test to check correct functionality with a simple test case
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
void test3() {
|
||||||
|
std::cout << "TEST CASE 3\n";
|
||||||
|
std::cout << "Intialized a = {4, 6} b = {2, 3}\n";
|
||||||
|
std::cout << "Expected result: {2, 3, 4, 6}\n";
|
||||||
|
std::vector<int32_t> a = {4, 6};
|
||||||
|
std::vector<int32_t> b = {2, 3};
|
||||||
|
std::vector<int32_t> result = get_union(a, b);
|
||||||
|
std::vector<int32_t> expected = {2, 3, 4, 6};
|
||||||
|
assert(result == expected); ///< Check if result is correct
|
||||||
|
print(result); ///< Should print 2 3 4 6
|
||||||
|
std::cout << "TEST PASSED!\n\n";
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @brief A Test to check correct functionality with duplicate values
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
void test4() {
|
||||||
|
std::cout << "TEST CASE 4\n";
|
||||||
|
std::cout << "Intialized a = {4, 6, 6, 7} b = {2, 3, 4}\n";
|
||||||
|
std::cout << "Expected result: {2, 3, 4, 6, 7}\n";
|
||||||
|
std::vector<int32_t> a = {4, 6, 6, 7};
|
||||||
|
std::vector<int32_t> b = {2, 3, 4};
|
||||||
|
std::vector<int32_t> result = get_union(a, b);
|
||||||
|
std::vector<int32_t> expected = {2, 3, 4, 6, 7};
|
||||||
|
assert(result == expected); ///< Check if result is correct
|
||||||
|
print(result); ///< Should print 2 3 4 6 7
|
||||||
|
std::cout << "TEST PASSED!\n\n";
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @brief A Test to check correct functionality with a harder test case
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
void test5() {
|
||||||
|
std::cout << "TEST CASE 5\n";
|
||||||
|
std::cout << "Intialized a = {1, 4, 6, 7, 9} b = {2, 3, 5}\n";
|
||||||
|
std::cout << "Expected result: {1, 2, 3, 4, 5, 6, 7, 9}\n";
|
||||||
|
std::vector<int32_t> a = {1, 4, 6, 7, 9};
|
||||||
|
std::vector<int32_t> b = {2, 3, 5};
|
||||||
|
std::vector<int32_t> result = get_union(a, b);
|
||||||
|
std::vector<int32_t> expected = {1, 2, 3, 4, 5, 6, 7, 9};
|
||||||
|
assert(result == expected); ///< Check if result is correct
|
||||||
|
print(result); ///< Should print 1 2 3 4 5 6 7 9
|
||||||
|
std::cout << "TEST PASSED!\n\n";
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @brief A Test to check correct functionality with an array sorted using
|
||||||
|
* std::sort
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
void test6() {
|
||||||
|
std::cout << "TEST CASE 6\n";
|
||||||
|
std::cout << "Intialized a = {1, 3, 3, 2, 5, 9, 4, 3, 2} ";
|
||||||
|
std::cout << "b = {11, 3, 7, 8, 6}\n";
|
||||||
|
std::cout << "Expected result: {1, 2, 3, 4, 5, 6, 7, 8, 9, 11}\n";
|
||||||
|
std::vector<int32_t> a = {1, 3, 3, 2, 5, 9, 4, 3, 2};
|
||||||
|
std::vector<int32_t> b = {11, 3, 7, 8, 6};
|
||||||
|
std::sort(a.begin(), a.end()); ///< Sort vector a
|
||||||
|
std::sort(b.begin(), b.end()); ///< Sort vector b
|
||||||
|
std::vector<int32_t> result = get_union(a, b);
|
||||||
|
std::vector<int32_t> expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11};
|
||||||
|
assert(result == expected); ///< Check if result is correct
|
||||||
|
print(result); ///< Should print 1 2 3 4 5 6 7 8 9 11
|
||||||
|
std::cout << "TEST PASSED!\n\n";
|
||||||
|
}
|
||||||
|
} // namespace tests
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Function to test the correctness of get_union() function
|
||||||
|
* @returns void
|
||||||
|
*/
|
||||||
|
static void test() {
|
||||||
|
tests::test1();
|
||||||
|
tests::test2();
|
||||||
|
tests::test3();
|
||||||
|
tests::test4();
|
||||||
|
tests::test5();
|
||||||
|
tests::test6();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief main function
|
||||||
|
* @returns 0 on exit
|
||||||
|
*/
|
||||||
|
int main() {
|
||||||
|
test(); // run self-test implementations
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user