TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
array_left_rotation.cpp
Go to the documentation of this file.
1
12
13
#include <cassert>
14
#include <iostream>
15
#include <vector>
16
21
namespace
operations_on_datastructures
{
22
29
void
print
(
const
std::vector<int32_t> &array) {
30
for
(int32_t i : array) {
31
std::cout << i <<
" "
;
32
}
33
std::cout <<
"\n"
;
34
}
35
46
std::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
68
namespace
tests
{
69
using
operations_on_datastructures::print
;
70
using
operations_on_datastructures::shift_left
;
75
void
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
}
86
90
void
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
}
101
105
void
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
}
116
120
void
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
}
139
143
void
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
159
static
void
test
() {
160
tests::test1
();
161
tests::test2
();
162
tests::test3
();
163
tests::test4
();
164
tests::test5
();
165
}
166
171
int
main
() {
172
test
();
// run self-test implementations
173
return
0;
174
}
test
void test()
Definition
caesar_cipher.cpp:100
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
operations_on_datastructures
for std::vector
operations_on_datastructures::print
void print(const std::vector< int32_t > &array)
Prints the values of a vector sequentially, ending with a newline character.
Definition
array_left_rotation.cpp:29
operations_on_datastructures::shift_left
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....
Definition
array_left_rotation.cpp:46
tests
Testcases to check Union of Two Arrays.
tests::test1
void test1()
A Test to check an simple case.
Definition
array_left_rotation.cpp:75
tests::test4
void test4()
A Test to check a very large input.
Definition
array_left_rotation.cpp:120
tests::print
void print(const std::vector< int32_t > &array)
Prints the values of a vector sequentially, ending with a newline character.
Definition
array_left_rotation.cpp:29
tests::test3
void test3()
A Test to check an invalid shift value.
Definition
array_left_rotation.cpp:105
tests::test2
void test2()
A Test to check an empty vector.
Definition
array_left_rotation.cpp:90
tests::test5
void test5()
A Test to check a shift of zero.
Definition
array_left_rotation.cpp:143
tests::shift_left
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....
Definition
array_left_rotation.cpp:46
operations_on_datastructures
array_left_rotation.cpp
Generated by
1.18.0