Skip to content

Commit 63f17fb

Browse files
committed
feat: [DSA,DP] Use tests.h
1 parent ff04a63 commit 63f17fb

File tree

4 files changed

+223
-402
lines changed

4 files changed

+223
-402
lines changed

cpp/dsa/extended-palindrome.cpp

100644100755
Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
// c++ code to get extended palindrome
1+
/**
2+
* Extended palindrome
3+
* ===================
4+
*
5+
* Refer:
6+
*
7+
* https://thejoboverflow.com/problem/337/
8+
*/
29

3-
#include <algorithm>
4-
#include <iostream>
5-
#include <vector>
10+
#include "tests.h"
611

7-
using namespace std;
8-
9-
// you can write to stdout for debugging purposes, e.g.
10-
// cout << "this is a debug message" << endl;
11-
12-
bool isp(string &s)
13-
{
14-
string p = s;
15-
reverse(p.begin(), p.end());
16-
return (s == p);
17-
}
12+
/* ===========================================================================
13+
* Algorithms implementation
14+
* ===========================================================================
15+
*/
16+
#define def_is_palin(T) \
17+
bool _is_palindrome(const T &s) \
18+
{ \
19+
T::const_iterator l, m; \
20+
T::const_reverse_iterator r; \
21+
l = begin(s), m = l + size(s) / 2, r = rbegin(s); \
22+
return equal(l, m, r); \
23+
}
1824

19-
bool isl(vector<string> &l)
20-
{
21-
vector<string> r = l;
22-
reverse(r.begin(), r.end());
23-
return (r == l);
24-
}
25+
def_is_palin(string);
26+
def_is_palin(vector<string>);
2527

26-
bool iscp(string s, int d)
28+
bool _iscp(string s, int d)
2729
{
2830
size_t cs = s.size() / d;
2931
size_t c = 0;
@@ -32,13 +34,12 @@ bool iscp(string s, int d)
3234
string p(s.begin() + c, s.begin() + c + d);
3335
l.push_back(p);
3436
}
35-
return isl(l);
37+
return _is_palindrome(l);
3638
}
3739

3840
int extendedPalindrome(string &s)
3941
{
40-
if (isp(s)) return 1;
41-
42+
if (_is_palindrome(s)) return 1;
4243
// get prime divisor of |s| or 1
4344
size_t d = 1;
4445
for (size_t i = 2; i < s.size(); i++) {
@@ -47,33 +48,25 @@ int extendedPalindrome(string &s)
4748
break;
4849
}
4950
}
50-
51-
if (iscp(s, d)) return d;
52-
51+
if (_iscp(s, d)) return d;
5352
return -1;
5453
}
5554

56-
// driver code
57-
int main(int, char **)
55+
/* ===========================================================================
56+
* Test code
57+
* ===========================================================================
58+
*/
59+
TEST(extendedPalindrome, "Extended palindrome")
5860
{
59-
vector<string> vl = {"radar", "abcxyzabc", "microsoft"};
60-
61-
if (getenv("SHOW_TEST_OUTPUT"))
62-
cout << "Testing implementation " << 1 << " "
63-
<< "extended palindrome"
64-
<< "\n";
65-
66-
for (int i = 0; i < static_cast<int>(vl.size()); i++) {
67-
string s = vl[i];
68-
int t = extendedPalindrome(s);
69-
70-
if (getenv("SHOW_TEST_OUTPUT"))
71-
cout << " test-" << i << ": "
72-
<< "input: str = " << vl[i]
73-
<< " output: num = " << t << "\n";
61+
vector<string> _i = {"radar", "abcxyzabc", "microsoft"};
62+
vi_t _e = {1, 3, -1};
63+
int n = size(_i);
64+
fii (i, n) {
65+
string &s = _i[i];
66+
int a = extendedPalindrome(s);
67+
CHECK_EQ(_e[i], a);
68+
SHOW_OUTPUT(s, a);
7469
}
75-
76-
cout << "Executed " << 1 << " implementations"
77-
<< " with " << 1 << " tests." << endl;
78-
return 0;
7970
}
71+
72+
INIT_TEST_MAIN();

cpp/dsa/str-to-num.cpp

100644100755
Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,65 @@
11
/**
2-
* Given a vector of strings representing unsigned numbers, convert the
3-
* numbers to a collection of std::uint16_t in which the odd numbers appear
4-
* before the even ones and output the data
2+
* Convert strings into integers & put odd ones before even
3+
* ========================================================
54
*/
65

7-
#include <bits/stdc++.h>
6+
#include "tests.h"
87

9-
using namespace std;
8+
/* ===========================================================================
9+
* Algorithms implementation
10+
* ===========================================================================
11+
*/
12+
static inline bool _is_number(const string &s)
13+
{
14+
return !empty(s) && all_of(begin(s), end(s), ::isdigit);
15+
}
1016

11-
auto input = std::vector<std::string>{
12-
"9", "0", "49", "2", "100", "not a number", "12 not a number too"};
17+
static inline bool _is_odd(int n) { return n % 2; }
18+
static inline bool _is_even(int n) { return !_is_odd(n); }
1319

14-
bool is_number(const std::string &s)
20+
void _moveOddToBegin(vi_t &a)
1521
{
16-
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
22+
int l = 0, r = size(a) - 1;
23+
while (l < r) {
24+
while (_is_even(a[l]) && l < r) l++;
25+
while (_is_odd(a[r]) && l < r) r--;
26+
if (l < r) swap(a[l++], a[r--]);
27+
}
1728
}
1829

19-
std::vector<std::uint16_t>
20-
getOddEvenNums(const std::vector<std::string> &input)
30+
vi_t oddEvens(const vector<string> &a)
2131
{
22-
std::vector<std::uint16_t> oen;
23-
for (auto &a : input) {
24-
std::uint16_t d = 0;
32+
vi_t ret;
33+
for (const auto &e : a) {
2534
try {
26-
if (is_number(a)) {
27-
d = static_cast<std::uint16_t>(std::stoul(a));
28-
oen.push_back(d);
29-
}
35+
if (_is_number(e)) ret.push_back(stoi(e));
3036
} catch (std::invalid_argument const &ex) {
31-
std::cout
32-
<< "std::invalid_argument::what(): " << ex.what()
33-
<< '\n';
37+
cerr << format("invalid argument: {}\n", ex.what());
3438
} catch (std::out_of_range const &ex) {
35-
std::cout
36-
<< "std::out_of_range::what(): " << ex.what()
37-
<< '\n';
39+
cerr << format("out of range: {}\n", ex.what());
3840
}
3941
}
40-
41-
std::size_t s = 0;
42-
std::size_t e = oen.size() - 1;
43-
while (s < e) {
44-
while (oen.at(s) % 2 == 0 && s < e) s++;
45-
while (oen.at(e) % 2 != 1 && s < e) e--;
46-
// similar to bubble sort swap odd & even numbers
47-
if (s < e) {
48-
std::swap(oen.at(s), oen.at(e));
49-
s++;
50-
e--;
51-
}
52-
}
53-
return oen;
42+
_moveOddToBegin(ret);
43+
return ret;
5444
}
5545

5646
/* ===========================================================================
5747
* Test code
5848
* ===========================================================================
5949
*/
60-
template <class T>
61-
string _vec2str(const vector<T> &vec)
50+
TEST(odd_evens, "Convert strings into integers & put odd ones before even")
6251
{
63-
ostringstream oss;
64-
oss << "{";
65-
copy(vec.begin(), vec.end() - 1, ostream_iterator<T>(oss, ", "));
66-
oss << vec.back();
67-
oss << "}";
68-
return oss.str();
52+
vector<string> i = {"9",
53+
"0",
54+
"49",
55+
"2",
56+
"100",
57+
"not a number",
58+
"12 not a number too"};
59+
vi_t e = {100, 0, 2, 49, 9};
60+
vi_t a = oddEvens(i);
61+
CHECK_EQ(e, a);
62+
SHOW_OUTPUT(i, a);
6963
}
7064

71-
int main(int, char **)
72-
{
73-
std::vector<std::uint16_t> oen = getOddEvenNums(input);
74-
75-
if (getenv("SHOW_TEST_OUTPUT"))
76-
cout << "Testing implementation " << 1 << " "
77-
<< "get odd & even numbers from vector string"
78-
<< "\n"
79-
<< " test-" << 1 << ": "
80-
<< "input: = " << _vec2str<string>(input)
81-
<< " output: oddEvenNums = " << _vec2str<uint16_t>(oen)
82-
<< "\n";
83-
84-
cout << "Executed " << 1 << " implementations"
85-
<< " with " << 1 << " tests." << endl;
86-
return 0;
87-
}
65+
INIT_TEST_MAIN();

cpp/dsa/third-max-num.cpp

100644100755
Lines changed: 18 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,12 @@
22
* Third distinct maximum number
33
* ==============================
44
*
5-
* Leetcode: [414. Third Maximum Number][lc414]
5+
* Refer:
66
*
7-
* Given an integer array nums, return the third distinct maximum number in
8-
* this array. If the third maximum does not exist, return the maximum number.
9-
*
10-
* Tags: #arrays, #easy
11-
*
12-
* Example 1:
13-
* Input: nums = [3,2,1]
14-
* Output: 1
15-
*
16-
* Example 2:
17-
* Input: nums = [3,2,1]
18-
* Output: 1
19-
*
20-
* Example 3:
21-
* Input: nums = [2,2,3,1]
22-
* Output: 1
23-
*
24-
* [lc414]: https://leetcode.com/problems/third-maximum-number/
7+
* https://leetcode.com/problems/third-maximum-number/
258
*/
269

27-
#include <bits/stdc++.h>
28-
29-
using namespace std;
30-
31-
/* ===========================================================================
32-
* Test helpers
33-
* ===========================================================================
34-
*/
35-
class _00_test
36-
{
37-
public:
38-
_00_test(const string &name) : name(name) {}
39-
40-
string getName(void) const { return name; }
41-
42-
virtual int thirdMax(const vector<int> &nums) = 0;
43-
44-
private:
45-
string name;
46-
};
10+
#include "tests.h"
4711

4812
/* ===========================================================================
4913
* Algorithms implementation
@@ -53,77 +17,28 @@ class _00_test
5317
/* TC : O(n log k) for k'th largest number
5418
* SC : O(1)
5519
*/
56-
class _01_set : public _00_test
20+
int thirdMax(const vi_t &a)
5721
{
58-
public:
59-
_01_set() : _00_test("Third max number using std::set") {}
60-
61-
int thirdMax(const vector<int> &a) override
62-
{
63-
set<int> s;
64-
for (int e : a) {
65-
s.insert(e);
66-
if (s.size() > 3) s.erase(*s.begin());
67-
}
68-
if (s.size() == 3) return *s.begin();
69-
return *prev(s.end());
22+
set<int> s;
23+
for (int e : a) {
24+
s.insert(e);
25+
if (s.size() > 3) s.erase(*s.begin());
7026
}
71-
};
27+
if (s.size() == 3) return *s.begin();
28+
return *prev(s.end());
29+
}
7230

7331
/* ===========================================================================
7432
* Test code
7533
* ===========================================================================
7634
*/
77-
string _vec2str(const vector<int> &vec)
35+
TEST(thirdMax, "Third distinct maximum number")
7836
{
79-
ostringstream oss;
80-
oss << "{";
81-
copy(vec.begin(), vec.end() - 1, ostream_iterator<int>(oss, ", "));
82-
oss << vec.back();
83-
oss << "}";
84-
return oss.str();
37+
vi_t i{2, 3, 2, 1};
38+
int e = 1;
39+
int a = thirdMax(i);
40+
CHECK_EQ(e, a);
41+
SHOW_OUTPUT(i, a);
8542
}
8643

87-
void test_impl(const vector<vector<int>> &ip, vector<int> &op,
88-
shared_ptr<_00_test> f)
89-
{
90-
for (size_t i = 0; i < ip.size(); i++) {
91-
int t = f->thirdMax(ip[i]);
92-
if (t != op[i]) {
93-
cerr << f->getName() << " test failed: "
94-
<< "expected " << op[i] << ", actual " << t
95-
<< "." << endl;
96-
exit(1);
97-
}
98-
99-
if (getenv("SHOW_TEST_OUTPUT"))
100-
cout << " test-" << i << ": "
101-
<< "input: nums = " << _vec2str(ip[i])
102-
<< " output: num = " << t << "\n";
103-
}
104-
}
105-
106-
int main(int, char **)
107-
{
108-
vector<vector<int>> ip{
109-
{2, 3, 2, 1},
110-
};
111-
112-
vector op{1};
113-
114-
vector<shared_ptr<_00_test>> impls{
115-
make_shared<_01_set>(),
116-
};
117-
118-
for (size_t i = 0; i < impls.size(); i++) {
119-
if (getenv("SHOW_TEST_OUTPUT"))
120-
cout << "Testing implementation " << i + 1 << " "
121-
<< impls[i]->getName() << "\n";
122-
123-
test_impl(ip, op, impls[i]);
124-
}
125-
126-
cout << "Executed " << impls.size() << " implementations"
127-
<< " with " << ip.size() << " tests." << endl;
128-
return 0;
129-
}
44+
INIT_TEST_MAIN();

0 commit comments

Comments
 (0)