mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-09 07:32:54 +08:00
Modified doc, fixed bug in shifting logic
Fixed a mistake that would cause the array to shift incorrectly
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Implementation for the [Union of two sorted
|
||||
* Arrays](https://en.wikipedia.org/wiki/Union_(set_theory))
|
||||
* @brief Implementation for the [Array Left
|
||||
* Rotation](https://www.javatpoint.com/program-to-left-rotate-the-elements-of-an-array)
|
||||
* 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).
|
||||
* @details Shifting an array to the left involves moving each element of the
|
||||
* array so that it occupies a position of a certain shift value before its
|
||||
* current one. This implementation uses a result vector and does not mutate the
|
||||
* input.
|
||||
* @author [Alvin](https://github.com/polarvoid)
|
||||
*/
|
||||
|
||||
@@ -41,19 +41,20 @@ void print(const std::vector<int32_t> &array) {
|
||||
* elements from the start of the vector.
|
||||
* @param array A reference to the input std::vector
|
||||
* @param shift The amount to be shifted to the left
|
||||
* @returns A std::vector with the shiftedd values
|
||||
* @returns A std::vector with the shifted values
|
||||
*/
|
||||
std::vector<int32_t> shift_left(const std::vector<int32_t> &array,
|
||||
size_t shift) {
|
||||
if (shift >= array.size()) {
|
||||
if (array.size() <= shift) {
|
||||
return {}; ///< We got an invalid shift, return empty array
|
||||
}
|
||||
std::vector<int32_t> res(array.size()); ///< Result array
|
||||
for (int i = shift; i < array.size(); i++) {
|
||||
res.push_back(array[i]); ///< Add values after the shift index
|
||||
res[i - shift] = array[i]; ///< Add values after the shift index
|
||||
}
|
||||
for (int i = 0; i < shift; i++) {
|
||||
res.push_back(array[i]); ///< Add the values from the start
|
||||
res[array.size() - shift + i] =
|
||||
array[i]; ///< Add the values from the start
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -68,20 +69,100 @@ namespace tests {
|
||||
using operations_on_datastructures::print;
|
||||
using operations_on_datastructures::shift_left;
|
||||
/**
|
||||
* @brief A Test to check an edge case (two empty arrays)
|
||||
* @brief A Test to check an simple case
|
||||
* @returns void
|
||||
*/
|
||||
void test1() {
|
||||
std::cout << "TEST CASE 1\n";
|
||||
std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n";
|
||||
std::cout << "Expected result: {3, 4, 5, 1, 2}\n";
|
||||
std::vector<int32_t> arr = {1, 2, 3, 4, 5};
|
||||
print(shift_left(arr, 2));
|
||||
std::vector<int32_t> res = shift_left(arr, 2);
|
||||
std::vector<int32_t> expected = {3, 4, 5, 1, 2};
|
||||
assert(res == expected);
|
||||
print(res); ///< Should print 3 4 5 1 2
|
||||
std::cout << "TEST PASSED!\n\n";
|
||||
}
|
||||
/**
|
||||
* @brief A Test to check an empty vector
|
||||
* @returns void
|
||||
*/
|
||||
void test2() {
|
||||
std::cout << "TEST CASE 2\n";
|
||||
std::cout << "Initialized arr = {}\n";
|
||||
std::cout << "Expected result: {}\n";
|
||||
std::vector<int32_t> arr = {};
|
||||
std::vector<int32_t> res = shift_left(arr, 2);
|
||||
std::vector<int32_t> expected = {};
|
||||
assert(res == expected);
|
||||
print(res); ///< Should print empty newline
|
||||
std::cout << "TEST PASSED!\n\n";
|
||||
}
|
||||
/**
|
||||
* @brief A Test to check an invalid shift value
|
||||
* @returns void
|
||||
*/
|
||||
void test3() {
|
||||
std::cout << "TEST CASE 3\n";
|
||||
std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n";
|
||||
std::cout << "Expected result: {}\n";
|
||||
std::vector<int32_t> arr = {1, 2, 3, 4, 5};
|
||||
std::vector<int32_t> res = shift_left(arr, 7); ///< 7 > 5
|
||||
std::vector<int32_t> expected = {};
|
||||
assert(res == expected);
|
||||
print(res); ///< Should print empty newline
|
||||
std::cout << "TEST PASSED!\n\n";
|
||||
}
|
||||
/**
|
||||
* @brief A Test to check a very large input
|
||||
* @returns void
|
||||
*/
|
||||
void test4() {
|
||||
std::cout << "TEST CASE 4\n";
|
||||
std::cout << "Initialized arr = {2, 4, ..., 420}\n";
|
||||
std::cout << "Expected result: {4, 6, ..., 420, 2}\n";
|
||||
std::vector<int32_t> arr;
|
||||
for (int i = 1; i <= 210; i++) {
|
||||
arr.push_back(i * 2);
|
||||
}
|
||||
print(arr);
|
||||
std::vector<int32_t> res = shift_left(arr, 1);
|
||||
std::vector<int32_t> expected;
|
||||
for (int i = 1; i < 210; i++) {
|
||||
expected.push_back(arr[i]);
|
||||
}
|
||||
expected.push_back(2);
|
||||
assert(res == expected);
|
||||
print(res); ///< Should print {4, 6, ..., 420, 2}
|
||||
std::cout << "TEST PASSED!\n\n";
|
||||
}
|
||||
/**
|
||||
* @brief A Test to check a shift of zero
|
||||
* @returns void
|
||||
*/
|
||||
void test5() {
|
||||
std::cout << "TEST CASE 5\n";
|
||||
std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n";
|
||||
std::cout << "Expected result: {1, 2, 3, 4, 5}\n";
|
||||
std::vector<int32_t> arr = {1, 2, 3, 4, 5};
|
||||
std::vector<int32_t> res = shift_left(arr, 0);
|
||||
assert(res == arr);
|
||||
print(res); ///< Should print 1 2 3 4 5
|
||||
std::cout << "TEST PASSED!\n\n";
|
||||
}
|
||||
} // namespace tests
|
||||
|
||||
/**
|
||||
* @brief Function to test the correctness of get_union() function
|
||||
* @brief Function to test the correctness of shift_left() function
|
||||
* @returns void
|
||||
*/
|
||||
static void test() { tests::test1(); }
|
||||
static void test() {
|
||||
tests::test1();
|
||||
tests::test2();
|
||||
tests::test3();
|
||||
tests::test4();
|
||||
tests::test5();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief main function
|
||||
|
||||
Reference in New Issue
Block a user