TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
array_left_rotation.cpp
Go to the documentation of this file.
1
13#include <cassert>
14#include <iostream>
15#include <vector>
16
22
29void print(const std::vector<int32_t> &array) {
30 for (int32_t i : array) {
31 std::cout << i << " ";
32 }
33 std::cout << "\n";
34}
35
46std::vector<int32_t> shift_left(const std::vector<int32_t> &array,
47 size_t shift) {
48 if (array.size() <= shift) {
49 return {};
50 }
51 std::vector<int32_t> res(array.size());
52 for (size_t i = shift; i < array.size(); i++) {
53 res[i - shift] = array[i];
54 }
55 for (size_t i = 0; i < shift; i++) {
56 res[array.size() - shift + i] =
57 array[i];
58 }
59 return res;
60}
61
62} // namespace operations_on_datastructures
63
68namespace tests {
75void test1() {
76 std::cout << "TEST CASE 1\n";
77 std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n";
78 std::cout << "Expected result: {3, 4, 5, 1, 2}\n";
79 std::vector<int32_t> arr = {1, 2, 3, 4, 5};
80 std::vector<int32_t> res = shift_left(arr, 2);
81 std::vector<int32_t> expected = {3, 4, 5, 1, 2};
82 assert(res == expected);
83 print(res);
84 std::cout << "TEST PASSED!\n\n";
85}
90void test2() {
91 std::cout << "TEST CASE 2\n";
92 std::cout << "Initialized arr = {}\n";
93 std::cout << "Expected result: {}\n";
94 std::vector<int32_t> arr = {};
95 std::vector<int32_t> res = shift_left(arr, 2);
96 std::vector<int32_t> expected = {};
97 assert(res == expected);
98 print(res);
99 std::cout << "TEST PASSED!\n\n";
100}
105void test3() {
106 std::cout << "TEST CASE 3\n";
107 std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n";
108 std::cout << "Expected result: {}\n";
109 std::vector<int32_t> arr = {1, 2, 3, 4, 5};
110 std::vector<int32_t> res = shift_left(arr, 7);
111 std::vector<int32_t> expected = {};
112 assert(res == expected);
113 print(res);
114 std::cout << "TEST PASSED!\n\n";
115}
120void test4() {
121 std::cout << "TEST CASE 4\n";
122 std::cout << "Initialized arr = {2, 4, ..., 420}\n";
123 std::cout << "Expected result: {4, 6, ..., 420, 2}\n";
124 std::vector<int32_t> arr;
125 for (int i = 1; i <= 210; i++) {
126 arr.push_back(i * 2);
127 }
128 print(arr);
129 std::vector<int32_t> res = shift_left(arr, 1);
130 std::vector<int32_t> expected;
131 for (int i = 1; i < 210; i++) {
132 expected.push_back(arr[i]);
133 }
134 expected.push_back(2);
135 assert(res == expected);
136 print(res);
137 std::cout << "TEST PASSED!\n\n";
138}
143void test5() {
144 std::cout << "TEST CASE 5\n";
145 std::cout << "Initialized arr = {1, 2, 3, 4, 5}\n";
146 std::cout << "Expected result: {1, 2, 3, 4, 5}\n";
147 std::vector<int32_t> arr = {1, 2, 3, 4, 5};
148 std::vector<int32_t> res = shift_left(arr, 0);
149 assert(res == arr);
150 print(res);
151 std::cout << "TEST PASSED!\n\n";
152}
153} // namespace tests
154
159static void test() {
160 tests::test1();
161 tests::test2();
162 tests::test3();
163 tests::test4();
164 tests::test5();
165}
166
171int main() {
172 test(); // run self-test implementations
173 return 0;
174}
static void test()
Function to test the correctness of shift_left() function.
int main()
main function
void print(const std::vector< int32_t > &array)
Prints the values of a vector sequentially, ending with a newline character.
std::vector< int32_t > shift_left(const std::vector< int32_t > &array, size_t shift)
Shifts the given vector to the left by the shift amount and returns a new vector with the result....
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.