Skip to content

Commit e7ae985

Browse files
committed
feat: [linked-lists] Rearrange split & alt merge
1 parent f9ca930 commit e7ae985

8 files changed

+591
-1278
lines changed

cpp/arrays/2pointers-01.cpp

Lines changed: 33 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
* ==========================================
44
*/
55

6-
#include <bits/stdc++.h>
7-
8-
using namespace std;
6+
#include "tests.h"
97

108
/* ===========================================================================
119
* Algorithms implementation
1210
* ===========================================================================
1311
*/
1412

15-
// Move all 0 to right / end of array in place with same order for other
16-
// elements
13+
#define _2p_move0toEnd_desc "Move all 0 to end / right in array"
1714

1815
/* TC : O(n)
1916
* SC : O(1)
@@ -22,149 +19,80 @@ void move0toEnd(span<int> a)
2219
{
2320
int n = a.size();
2421
int j = 0;
25-
for (int i = 0; i < n; i++) {
26-
if (a[i] != 0) {
27-
swap(a[j++], a[i]); // Partitioning the array
28-
}
29-
}
22+
fii (i, n)
23+
if (a[i]) swap(a[j++], a[i]);
3024
}
3125

32-
// Move all 0 to left / begin of array in place with same order for other
33-
// elements
26+
#define _2p_move0toBegin_desc "Move all 0 to begin / left in array"
3427

28+
/* TC : O(n)
29+
* SC : O(1)
30+
*/
3531
void move0toBegin(span<int> a)
3632
{
3733
int n = a.size();
3834
int j = n - 1;
39-
for (int i = n - 1; i >= 0; i--) {
40-
if (a[i] != 0) {
41-
swap(a[j--], a[i]); // Partitioning the array
42-
}
43-
}
35+
frii (i, j)
36+
if (a[i]) swap(a[j--], a[i]);
4437
}
4538

46-
// Remove repeated / duplicates in sorted array
39+
#define _2p_rmDupsSorted_desc "Remove duplicates in sorted array"
4740

48-
void removeDuplicatesSorted(span<int> a)
41+
/* TC : O(n)
42+
* SC : O(1)
43+
*/
44+
void rmDupsSorted(span<int> a)
4945
{
5046
int n = a.size();
5147
if (n == 0 || n == 1) return;
5248

5349
int j = 0;
54-
for (int i = 0; i < n - 1; i++)
50+
fii (i, n - 1)
5551
if (a[i] != a[i + 1]) a[j++] = a[i];
5652
a[j++] = a[n - 1];
5753

5854
// Remove or zero all remaining elements
5955
for (; j < n; j++) a[j] = 0;
6056
}
6157

62-
/* ===========================================================================
63-
* Test helpers
64-
* ===========================================================================
65-
*/
66-
template <class T>
67-
ostream &operator<<(ostream &out, const vector<T> &c)
68-
{
69-
out << "{";
70-
for (int i = 0; const auto &e : c) out << (i++ ? ", " : "") << e;
71-
out << "}";
72-
return out;
73-
}
74-
75-
struct _00_test {
76-
_00_test(const string &name) : name(name) {}
77-
string getName(void) const { return name; }
78-
virtual int test_func() = 0;
79-
80-
private:
81-
string name;
82-
};
83-
84-
#define CHECK_EQ(l, r) \
85-
if (l != r) { \
86-
cerr << this->getName() << ": test-" << tc + 1 << " failed!" \
87-
<< " expected " << e << ", actual " << a << "." \
88-
<< endl; \
89-
exit(1); \
90-
} else { \
91-
tc++; \
92-
if (getenv("SHOW_TEST_OUTPUT")) \
93-
cout << " test-" << tc << ": " \
94-
<< "input: " << a << " output: " << e << "\n"; \
95-
}
96-
97-
vector<_00_test *> tests;
98-
99-
#define _TEST_FUNC(func) test_##func
100-
101-
#define TEST(func, name) \
102-
struct _TEST_FUNC(func) : public _00_test { \
103-
int test_func() override; \
104-
\
105-
_TEST_FUNC(func)() : _00_test(name) \
106-
{ \
107-
tests.push_back(this); \
108-
} \
109-
}; \
110-
_TEST_FUNC(func) _test_instance_##func; \
111-
int _TEST_FUNC(func)::test_func()
112-
113-
#define RUN_ALL_TESTS() \
114-
size_t tc = 0, ts = 0; \
115-
for (; ts < tests.size(); ts++) { \
116-
if (getenv("SHOW_TEST_OUTPUT")) \
117-
cout << "Testing implementation " << ts + 1 << " " \
118-
<< tests[ts]->getName() << "\n"; \
119-
tc += tests[ts]->test_func(); \
120-
} \
121-
\
122-
cout << "Executed " << ts << " implementations" \
123-
<< " with " << tc << " tests." << endl;
124-
12558
/* ===========================================================================
12659
* Test code
12760
* ===========================================================================
12861
*/
129-
TEST(move0toEnd, "Move all 0 to end / right in array")
62+
TEST(move0toEnd, _2p_move0toEnd_desc)
13063
{
131-
int tc = 0;
132-
vector<int> a, e;
133-
a = {5, 6, 0, 4, 6, 0, 9, 0, 8};
64+
vi_t i, a, e;
65+
i = {5, 6, 0, 4, 6, 0, 9, 0, 8};
66+
a = i;
13467
e = {5, 6, 4, 6, 9, 8, 0, 0, 0};
13568
move0toEnd(a);
13669

13770
CHECK_EQ(e, a);
138-
return tc;
71+
SHOW_OUTPUT(i, a);
13972
}
14073

141-
TEST(move0toBegin, "Move all 0 to begin / left in array")
74+
TEST(move0toBegin, _2p_move0toBegin_desc)
14275
{
143-
int tc = 0;
144-
vector<int> a, e;
145-
a = {5, 6, 0, 4, 6, 0, 9, 0, 8};
76+
vi_t i, a, e;
77+
i = {5, 6, 0, 4, 6, 0, 9, 0, 8};
78+
a = i;
14679
e = {0, 0, 0, 5, 6, 4, 6, 9, 8};
14780
move0toBegin(a);
14881

14982
CHECK_EQ(e, a);
150-
return tc;
83+
SHOW_OUTPUT(i, a);
15184
}
15285

153-
TEST(removeDuplicatesSorted, "Remove duplicates in sorted array")
86+
TEST(rmDupsSorted, _2p_rmDupsSorted_desc)
15487
{
155-
156-
int tc = 0;
157-
vector<int> a, e;
158-
a = {1, 2, 2, 3, 4, 4, 4, 5, 5};
88+
vi_t i, a, e;
89+
i = {1, 2, 2, 3, 4, 4, 4, 5, 5};
90+
a = i;
15991
e = {1, 2, 3, 4, 5, 0, 0, 0, 0};
160-
removeDuplicatesSorted(a);
92+
rmDupsSorted(a);
16193

16294
CHECK_EQ(e, a);
163-
return tc;
95+
SHOW_OUTPUT(i, a);
16496
}
16597

166-
int main(int, char **)
167-
{
168-
RUN_ALL_TESTS();
169-
return 0;
170-
}
98+
INIT_TEST_MAIN();

cpp/arrays/2sum-01.cpp

Lines changed: 30 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,114 +7,56 @@
77
* https://leetcode.com/problems/two-sum/
88
*/
99

10-
#include <bits/stdc++.h>
11-
12-
using namespace std;
13-
14-
/* ===========================================================================
15-
* Test helpers
16-
* ===========================================================================
17-
*/
18-
class _00_test
19-
{
20-
public:
21-
_00_test(const string &name) : name(name) {}
22-
23-
string getName(void) const { return name; }
24-
25-
virtual vector<int> twoSum(vector<int> &nums, int target) = 0;
26-
27-
private:
28-
string name;
29-
};
10+
#include "tests.h"
3011

3112
/* ===========================================================================
3213
* Algorithms implementation
3314
* ===========================================================================
3415
*/
3516

17+
#define _2sum_desc "Two sum - sum of 2 numbers in array to get target"
18+
3619
/* TC : O(n)
3720
* SC : O(n)
3821
*/
39-
class _01_2sum : public _00_test
22+
vi_t twoSum(const vi_t &a, int t)
4023
{
41-
public:
42-
_01_2sum() : _00_test("Two sum") {}
43-
44-
vector<int> twoSum(vector<int> &a, int t) override
45-
{
46-
int n = static_cast<int>(a.size());
47-
unordered_map<int, int> m;
48-
for (int i = 0; i < n; i++) {
49-
int c = t - a[i];
50-
if (m.contains(c)) return {m[c], i};
51-
m[a[i]] = i;
52-
}
53-
return {};
24+
int n = a.size();
25+
umi_t m;
26+
fii (i, n) {
27+
int c = t - a[i];
28+
if (m.contains(c)) return {m[c], i};
29+
m[a[i]] = i;
5430
}
55-
};
31+
return {};
32+
}
5633

5734
/* ===========================================================================
5835
* Test code
5936
* ===========================================================================
6037
*/
61-
string _vec2str(vector<int> &vec)
62-
{
63-
ostringstream oss;
64-
oss << "{";
65-
copy(vec.begin(), vec.end() - 1, ostream_iterator<int>(oss, ", "));
66-
oss << vec.back();
67-
oss << "}";
68-
return oss.str();
69-
}
70-
71-
void test_impl(vector<pair<vector<int>, int>> &ip, vector<vector<int>> &op,
72-
shared_ptr<_00_test> f)
73-
{
74-
for (size_t i = 0; i < ip.size(); i++) {
75-
vector<int> t = f->twoSum(ip[i].first, ip[i].second);
76-
if (t != op[i]) {
77-
cerr << f->getName() << " test failed: "
78-
<< "expected " << _vec2str(op[i]) << ", actual "
79-
<< _vec2str(t) << "." << endl;
80-
exit(1);
81-
}
82-
83-
if (getenv("SHOW_TEST_OUTPUT"))
84-
cout << " test-" << i << ": "
85-
<< "input: nums = " << _vec2str(ip[i].first)
86-
<< ", target = " << ip[i].second
87-
<< " output: indices = " << _vec2str(t) << "\n";
38+
#define _2sum_check(i, t, e) \
39+
{ \
40+
vi_t a = twoSum(i, t); \
41+
CHECK_EQ(e, a); \
42+
SHOW_OUTPUT(i, a) \
8843
}
89-
}
9044

91-
int main(int, char **)
45+
TEST(twoSum, _2sum_desc)
9246
{
93-
vector<pair<vector<int>, int>> ip{
94-
{{2, 7, 11, 15}, 9},
95-
{{3, 2, 4}, 6},
96-
{{3, 3}, 6},
97-
};
98-
99-
vector<vector<int>> op{
100-
{0, 1},
101-
{1, 2},
102-
{0, 1},
103-
};
47+
vi_t i, e;
10448

105-
vector<shared_ptr<_00_test>> impls{
106-
make_shared<_01_2sum>(),
107-
};
49+
i = {2, 7, 11, 15};
50+
e = {0, 1};
51+
_2sum_check(i, 9, e);
10852

109-
for (size_t i = 0; i < impls.size(); i++) {
110-
if (getenv("SHOW_TEST_OUTPUT"))
111-
cout << "Testing implementation " << i + 1 << " "
112-
<< impls[i]->getName() << "\n";
53+
i = {3, 2, 4};
54+
e = {1, 2};
55+
_2sum_check(i, 6, e);
11356

114-
test_impl(ip, op, impls[i]);
115-
}
116-
117-
cout << "Executed " << impls.size() << " implementations"
118-
<< " with " << ip.size() << " tests." << endl;
119-
return 0;
57+
i = {3, 3};
58+
e = {0, 1};
59+
_2sum_check(i, 6, e);
12060
}
61+
62+
INIT_TEST_MAIN();

0 commit comments

Comments
 (0)