TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
array_right_rotation.cpp
Go to the documentation of this file.
1
14#include <cassert>
15#include <iostream>
16#include <vector>
17
23
30void print(const std::vector<int32_t> &array) {
31 for (int32_t i : array) {
32 std::cout << i << " ";
33 }
34 std::cout << "\n";
35}
36
47std::vector<int32_t> shift_right(const std::vector<int32_t> &array,
48 size_t shift) {
49 if (array.size() <= shift) {
50 return {};
51 }
52 std::vector<int32_t> res(array.size());
53 for (size_t i = shift; i < array.size(); i++) {
54 res[i] = array[i - shift];
55 }
56 for (size_t i = 0; i < shift; i++) {
57 res[i] =
58 array[array.size() - shift + i];
59 }
60 return res;
61}
62
63} // namespace operations_on_datastructures
64
69namespace tests {
76void 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);
85 std::cout << "TEST PASSED!\n\n";
86}
91void 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);
100 std::cout << "TEST PASSED!\n\n";
101}
106void 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);
112 std::vector<int32_t> expected = {};
113 assert(res == expected);
114 print(res);
115 std::cout << "TEST PASSED!\n\n";
116}
121void 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);
128 }
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]);
135 }
136 assert(res == expected);
137 print(res);
138 std::cout << "TEST PASSED!\n\n";
139}
144void 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);
152 std::cout << "TEST PASSED!\n\n";
153}
154} // namespace tests
155
160static void test() {
161 tests::test1();
162 tests::test2();
163 tests::test3();
164 tests::test4();
165 tests::test5();
166}
167
172int main() {
173 test(); // run self-test implementations
174 return 0;
175}
static void test()
Function to test the correctness of shift_right() function.
int main()
main function
std::vector< int32_t > shift_right(const std::vector< int32_t > &array, size_t shift)
Shifts the given vector to the right by the shift amount and returns a new vector with the result....
void print(const std::vector< int32_t > &array)
Prints the values of a vector sequentially, ending with a newline character.
Testcases to check Union of Two Arrays.
void test1()
A Test to check an simple case.
void test4()
A Test to check a very large input.
void test3()
A Test to check an invalid shift value.
void test2()
A Test to check an empty vector.
void test5()
A Test to check a shift of zero.