|
1 |
| -#include <iostream> |
2 |
| -using namespace std; |
3 |
| -int main() { |
4 |
| - int n, k; |
5 |
| - cout << "Enter size of array=\t"; |
6 |
| - cin >> n; |
7 |
| - cout << "Enter Number of indices u want to rotate the array to right=\t"; |
8 |
| - cin >> k; |
9 |
| - int a[n]; |
10 |
| - cout << "Enter elements of array=\t"; |
11 |
| - for (int i = 0; i < n; i++) cin >> a[i]; |
12 |
| - int temp = 0; |
13 |
| - for (int i = 0; i < k; i++) { |
14 |
| - temp = a[n - 1]; |
15 |
| - for (int j = n - 1; j >= 0; j--) { |
16 |
| - if (j == 0) { |
17 |
| - a[j] = temp; |
18 |
| - } else { |
19 |
| - a[j] = a[j - 1]; |
20 |
| - } |
21 |
| - } |
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation for the [Array right |
| 4 | + * Rotation](https://www.javatpoint.com/program-to-right-rotate-the-elements-of-an-array) |
| 5 | + * algorithm. |
| 6 | + * @details Shifting an array to the right involves moving each element of the |
| 7 | + * array so that it occupies a position of a certain shift value after its |
| 8 | + * current one. This implementation uses a result vector and does not mutate the |
| 9 | + * input. |
| 10 | + * @see array_left_rotation.cpp |
| 11 | + * @author [Alvin](https://github.com/polarvoid) |
| 12 | + */ |
| 13 | + |
| 14 | +#include <cassert> /// for assert |
| 15 | +#include <iostream> /// for IO operations |
| 16 | +#include <vector> /// for std::vector |
| 17 | + |
| 18 | +/** |
| 19 | + * @namespace operations_on_datastructures |
| 20 | + * @brief Operations on Data Structures |
| 21 | + */ |
| 22 | +namespace operations_on_datastructures { |
| 23 | + |
| 24 | +/** |
| 25 | + * @brief Prints the values of a vector sequentially, ending with a newline |
| 26 | + * character. |
| 27 | + * @param array Reference to the array to be printed |
| 28 | + * @returns void |
| 29 | + */ |
| 30 | +void print(const std::vector<int32_t> &array) { |
| 31 | + for (int32_t i : array) { |
| 32 | + std::cout << i << " "; /// Print each value in the array |
| 33 | + } |
| 34 | + std::cout << "\n"; /// Print newline |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * @brief Shifts the given vector to the right by the shift amount and returns a |
| 39 | + * new vector with the result. The original vector is not mutated. |
| 40 | + * @details Shifts the values of the vector, by creating a new vector and adding |
| 41 | + * values from the shift index to the end, then appending the rest of the |
| 42 | + * elements to the start of the vector. |
| 43 | + * @param array A reference to the input std::vector |
| 44 | + * @param shift The amount to be shifted to the right |
| 45 | + * @returns A std::vector with the shifted values |
| 46 | + */ |
| 47 | +std::vector<int32_t> shift_right(const std::vector<int32_t> &array, |
| 48 | + size_t shift) { |
| 49 | + if (array.size() <= shift) { |
| 50 | + return {}; ///< We got an invalid shift, return empty array |
| 51 | + } |
| 52 | + std::vector<int32_t> res(array.size()); ///< Result array |
| 53 | + for (size_t i = shift; i < array.size(); i++) { |
| 54 | + res[i] = array[i - shift]; ///< Add values after the shift index |
| 55 | + } |
| 56 | + for (size_t i = 0; i < shift; i++) { |
| 57 | + res[i] = |
| 58 | + array[array.size() - shift + i]; ///< Add the values from the start |
| 59 | + } |
| 60 | + return res; |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace operations_on_datastructures |
| 64 | + |
| 65 | +/** |
| 66 | + * @namespace tests |
| 67 | + * @brief Testcases to check Union of Two Arrays. |
| 68 | + */ |
| 69 | +namespace tests { |
| 70 | +using operations_on_datastructures::print; |
| 71 | +using operations_on_datastructures::shift_right; |
| 72 | +/** |
| 73 | + * @brief A Test to check an simple case |
| 74 | + * @returns void |
| 75 | + */ |
| 76 | +void test1() { |
| 77 | + std::cout << "TEST CASE 1\n"; |
| 78 | + std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n"; |
| 79 | + std::cout << "Expected result: {4, 5, 1, 2, 3}\n"; |
| 80 | + std::vector<int32_t> arr = {1, 2, 3, 4, 5}; |
| 81 | + std::vector<int32_t> res = shift_right(arr, 2); |
| 82 | + std::vector<int32_t> expected = {4, 5, 1, 2, 3}; |
| 83 | + assert(res == expected); |
| 84 | + print(res); ///< Should print 4 5 1 2 3 |
| 85 | + std::cout << "TEST PASSED!\n\n"; |
| 86 | +} |
| 87 | +/** |
| 88 | + * @brief A Test to check an empty vector |
| 89 | + * @returns void |
| 90 | + */ |
| 91 | +void test2() { |
| 92 | + std::cout << "TEST CASE 2\n"; |
| 93 | + std::cout << "Initialized arr = {}\n"; |
| 94 | + std::cout << "Expected result: {}\n"; |
| 95 | + std::vector<int32_t> arr = {}; |
| 96 | + std::vector<int32_t> res = shift_right(arr, 2); |
| 97 | + std::vector<int32_t> expected = {}; |
| 98 | + assert(res == expected); |
| 99 | + print(res); ///< Should print empty newline |
| 100 | + std::cout << "TEST PASSED!\n\n"; |
| 101 | +} |
| 102 | +/** |
| 103 | + * @brief A Test to check an invalid shift value |
| 104 | + * @returns void |
| 105 | + */ |
| 106 | +void test3() { |
| 107 | + std::cout << "TEST CASE 3\n"; |
| 108 | + std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n"; |
| 109 | + std::cout << "Expected result: {}\n"; |
| 110 | + std::vector<int32_t> arr = {1, 2, 3, 4, 5}; |
| 111 | + std::vector<int32_t> res = shift_right(arr, 7); ///< 7 > 5 |
| 112 | + std::vector<int32_t> expected = {}; |
| 113 | + assert(res == expected); |
| 114 | + print(res); ///< Should print empty newline |
| 115 | + std::cout << "TEST PASSED!\n\n"; |
| 116 | +} |
| 117 | +/** |
| 118 | + * @brief A Test to check a very large input |
| 119 | + * @returns void |
| 120 | + */ |
| 121 | +void test4() { |
| 122 | + std::cout << "TEST CASE 4\n"; |
| 123 | + std::cout << "Initialized arr = {2, 4, ..., 420}\n"; |
| 124 | + std::cout << "Expected result: {420, 2, 4, ..., 418}\n"; |
| 125 | + std::vector<int32_t> arr; |
| 126 | + for (int i = 1; i <= 210; i++) { |
| 127 | + arr.push_back(i * 2); |
22 | 128 | }
|
23 |
| - cout << "Your rotated array is=\t"; |
24 |
| - for (int i = 0; i < n; i++) { |
25 |
| - cout << a[i] << " "; |
| 129 | + print(arr); |
| 130 | + std::vector<int32_t> res = shift_right(arr, 1); |
| 131 | + std::vector<int32_t> expected; |
| 132 | + expected.push_back(420); |
| 133 | + for (int i = 0; i < 209; i++) { |
| 134 | + expected.push_back(arr[i]); |
26 | 135 | }
|
| 136 | + assert(res == expected); |
| 137 | + print(res); ///< Should print {420, 2, 4, ..., 418} |
| 138 | + std::cout << "TEST PASSED!\n\n"; |
| 139 | +} |
| 140 | +/** |
| 141 | + * @brief A Test to check a shift of zero |
| 142 | + * @returns void |
| 143 | + */ |
| 144 | +void test5() { |
| 145 | + std::cout << "TEST CASE 5\n"; |
| 146 | + std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n"; |
| 147 | + std::cout << "Expected result: {1, 2, 3, 4, 5}\n"; |
| 148 | + std::vector<int32_t> arr = {1, 2, 3, 4, 5}; |
| 149 | + std::vector<int32_t> res = shift_right(arr, 0); |
| 150 | + assert(res == arr); |
| 151 | + print(res); ///< Should print 1 2 3 4 5 |
| 152 | + std::cout << "TEST PASSED!\n\n"; |
| 153 | +} |
| 154 | +} // namespace tests |
| 155 | + |
| 156 | +/** |
| 157 | + * @brief Function to test the correctness of shift_right() function |
| 158 | + * @returns void |
| 159 | + */ |
| 160 | +static void test() { |
| 161 | + tests::test1(); |
| 162 | + tests::test2(); |
| 163 | + tests::test3(); |
| 164 | + tests::test4(); |
| 165 | + tests::test5(); |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * @brief main function |
| 170 | + * @returns 0 on exit |
| 171 | + */ |
| 172 | +int main() { |
| 173 | + test(); // run self-test implementations |
| 174 | + return 0; |
27 | 175 | }
|
0 commit comments