Skip to content

Commit 3af7794

Browse files
TsungHsienLeeTsungHsienLee
TsungHsienLee
authored and
TsungHsienLee
committed
refactor code
1 parent 1c5d96e commit 3af7794

File tree

69 files changed

+1231
-794
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1231
-794
lines changed

Approximate_sort.cpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
3+
#include <functional>
14
#include <iostream>
2-
#include <string>
5+
#include <queue>
6+
#include <random>
37
#include <sstream>
8+
#include <string>
49
#include <vector>
5-
#include <ctime>
6-
#include <cstdlib>
7-
#include <queue>
810

9-
using namespace std;
11+
using std::cout;
12+
using std::default_random_engine;
13+
using std::endl;
14+
using std::greater;
15+
using std::istringstream;
16+
using std::priority_queue;
17+
using std::random_device;
18+
using std::string;
19+
using std::stringstream;
20+
using std::uniform_int_distribution;
21+
using std::vector;
1022

1123
// @include
1224
template <typename T>
13-
void approximate_sort(istringstream &sin, const int &k) {
25+
void approximate_sort(istringstream &sin, int k) {
1426
priority_queue<T, vector<T>, greater<T>> min_heap;
15-
// Firstly push k elements into min_heap
27+
// Firstly push k elements into min_heap.
1628
T x;
1729
for (int i = 0; i < k && sin >> x; ++i) {
1830
min_heap.push(x);
1931
}
2032

21-
// Extract the minimum one for every incoming element
33+
// Extract the minimum one for every incoming element.
2234
while (sin >> x) {
2335
min_heap.push(x);
2436
cout << min_heap.top() << endl;
2537
min_heap.pop();
2638
}
2739

28-
// Extract the remaining elements in min_heap
40+
// Extract the remaining elements in min_heap.
2941
while (!min_heap.empty()) {
3042
cout << min_heap.top() << endl;
3143
min_heap.pop();
3244
}
3345
}
3446
// @exclude
3547

36-
// It should print 1, 2, 3, 4, 5, 6, 7, ,8, 9
48+
// It should print 1, 2, 3, 4, 5, 6, 7, ,8, 9.
3749
void simple_test(void) {
3850
vector<int> A = {2, 1, 5, 4, 3, 9, 8, 7, 6};
3951
stringstream ss;
@@ -46,22 +58,26 @@ void simple_test(void) {
4658

4759
int main(int argc, char *argv[]) {
4860
simple_test();
49-
srand(time(nullptr));
61+
default_random_engine gen((random_device())());
5062
int n, k;
5163
if (argc == 2) {
5264
n = atoi(argv[1]);
53-
k = 1 + rand() % n;
65+
uniform_int_distribution<int> dis(1, n);
66+
k = dis(gen);
5467
} else if (argc == 3) {
5568
n = atoi(argv[1]);
5669
k = atoi(argv[2]);
5770
} else {
58-
n = 1 + rand() % 100000;
59-
k = 1 + rand() % n;
71+
uniform_int_distribution<int> n_dis(1, 100000);
72+
n = n_dis(gen);
73+
uniform_int_distribution<int> k_dis(1, n);
74+
k = k_dis(gen);
6075
}
6176
cout << "n = " << n << " k = " << k << endl;
6277
vector<int> A;
78+
uniform_int_distribution<int> dis(1, 999999);
6379
for (int i = 0; i < n; ++i) {
64-
A.push_back(rand());
80+
A.push_back(dis(gen));
6581
}
6682
stringstream ss;
6783
for (const int &a : A) {

BST_sorted_order_template.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
#include "BST_prototype.h"
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
23
#include <iostream>
34
#include <stack>
45

5-
using namespace std;
6+
#include "./BST_prototype.h"
7+
8+
using std::cout;
9+
using std::endl;
10+
using std::stack;
611

712
// @include
813
template <typename T>
@@ -28,12 +33,18 @@ int main(int argc, char *argv[]) {
2833
// 3
2934
// 2 5
3035
// 1 4 6
31-
shared_ptr<BinarySearchTree<int>> root = shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{3, nullptr});
32-
root->left = shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{2, nullptr});
33-
root->left->left = shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{1, nullptr});
34-
root->right = shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{5, nullptr});
35-
root->right->left = shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{4, nullptr});
36-
root->right->right = shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{6, nullptr});
36+
shared_ptr<BinarySearchTree<int>> root =
37+
shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{3, nullptr});
38+
root->left =
39+
shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{2, nullptr});
40+
root->left->left =
41+
shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{1, nullptr});
42+
root->right =
43+
shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{5, nullptr});
44+
root->right->left =
45+
shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{4, nullptr});
46+
root->right->right =
47+
shared_ptr<BinarySearchTree<int>>(new BinarySearchTree<int>{6, nullptr});
3748
// should output 1 2 3 4 5 6
3849
print_BST_in_sorted_order<int>(root);
3950
return 0;

Balanced_binary_tree_template.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1-
#include "Binary_tree_prototype_template.h"
2-
#include <iostream>
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
3+
#include <algorithm>
34
#include <cassert>
45
#include <cstdlib>
6+
#include <iostream>
7+
8+
#include "./Binary_tree_prototype_template.h"
59

6-
using namespace std;
10+
using std::boolalpha;
11+
using std::cout;
12+
using std::endl;
13+
using std::max;
714

815
// @include
916
template <typename T>
1017
int get_height(const shared_ptr<BinaryTree<T>> &n) {
1118
if (!n) {
12-
return -1; // base case
19+
return -1; // base case.
1320
}
1421

1522
int l_height = get_height(n->left);
1623
if (l_height == -2) {
17-
return -2; // left subtree is not balanced
24+
return -2; // left subtree is not balanced.
1825
}
1926
int r_height = get_height(n->right);
2027
if (r_height == -2) {
21-
return -2; // right subtree is not balanced
28+
return -2; // right subtree is not balanced.
2229
}
2330

2431
if (abs(l_height - r_height) > 1) {
25-
return -2; // current node n is not balanced
32+
return -2; // current node n is not balanced.
2633
}
27-
return max(l_height, r_height) + 1; // return the height
34+
return max(l_height, r_height) + 1; // return the height.
2835
}
2936

3037
template <typename T>
@@ -38,7 +45,8 @@ int main(int argc, char *argv[]) {
3845
// 3
3946
// 2 5
4047
// 1 4 6
41-
shared_ptr<BinaryTree<int>> root = shared_ptr<BinaryTree<int>>(new BinaryTree<int>());
48+
shared_ptr<BinaryTree<int>> root =
49+
shared_ptr<BinaryTree<int>>(new BinaryTree<int>());
4250
root->left = shared_ptr<BinaryTree<int>>(new BinaryTree<int>());
4351
root->left->left = shared_ptr<BinaryTree<int>>(new BinaryTree<int>());
4452
root->right = shared_ptr<BinaryTree<int>>(new BinaryTree<int>());

Binary_search_Ai=i.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
#include <iostream>
2-
#include <cstdlib>
3-
#include <ctime>
4-
#include <vector>
5-
#include <cassert>
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
63
#include <algorithm>
7-
#ifdef __clang__
4+
#include <cassert>
5+
#include <iostream>
6+
#include <random>
87
#include <unordered_set>
9-
#else
10-
#include <tr1/unordered_set>
11-
#endif
8+
#include <vector>
129

13-
using namespace std;
14-
#ifndef __clang__
15-
using namespace std::tr1;
16-
#endif
10+
using std::cout;
11+
using std::default_random_engine;
12+
using std::endl;
13+
using std::random_device;
14+
using std::uniform_int_distribution;
15+
using std::unordered_set;
16+
using std::vector;
1717

1818
// @include
1919
int search_index_value_equal(const vector<int> &A) {
@@ -45,21 +45,24 @@ int check_ans(const vector<int> &A) {
4545
}
4646

4747
int main(int argc, char *argv[]) {
48-
srand(time(nullptr));
48+
default_random_engine gen((random_device())());
4949
for (int times = 0; times < 1000; ++times) {
5050
int n;
5151
vector<int> A;
5252
unordered_set<int> table;
5353
if (argc == 2) {
5454
n = atoi(argv[1]);
5555
} else {
56-
n = 1 + rand() % 1000;
56+
uniform_int_distribution<int> dis(1, 1000);
57+
n = dis(gen);
5758
}
5859
for (int i = 0; i < n; ++i) {
5960
int x;
6061
unordered_set<int>::iterator iter;
6162
do {
62-
x = ((rand() & 1) ? -1 : 1) * rand() % 1000;
63+
uniform_int_distribution<int> pos_or_neg(0, 1);
64+
uniform_int_distribution<int> dis(0, 999);
65+
x = ((pos_or_neg(gen) & 1) ? -1 : 1) * dis(gen);
6366
iter = table.find(x);
6467
} while (iter != table.cend());
6568
table.emplace_hint(iter, x);

Binary_search_circular_array_template.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
#include <iostream>
2-
#include <cassert>
3-
#include <vector>
4-
#include <cstdlib>
5-
#include <ctime>
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
63
#include <algorithm>
7-
#ifdef __clang__
4+
#include <cassert>
5+
#include <iostream>
6+
#include <random>
87
#include <unordered_set>
9-
#else
10-
#include <tr1/unordered_set>
11-
#endif
8+
#include <vector>
129

13-
using namespace std;
14-
#ifndef __clang__
15-
using namespace std::tr1;
16-
#endif
10+
using std::cout;
11+
using std::default_random_engine;
12+
using std::endl;
13+
using std::random_device;
14+
using std::uniform_int_distribution;
15+
using std::unordered_set;
16+
using std::vector;
1717

1818
// @include
1919
template <typename T>
@@ -32,27 +32,30 @@ int search_smallest(const vector<T> &A) {
3232
// @exclude
3333

3434
int main(int argc, char *argv[]) {
35-
srand(time(nullptr));
35+
default_random_engine gen((random_device())());
3636
for (int times = 0; times < 1000; ++times) {
3737
int n;
3838
if (argc == 2) {
3939
n = atoi(argv[1]);
4040
} else {
41-
n = 1 + rand() % 10000;
41+
uniform_int_distribution<int> dis(1, 10000);
42+
n = dis(gen);
4243
}
4344
vector<int> A;
4445
unordered_set<int> table;
4546
for (size_t i = 0; i < n; ++i) {
4647
while (true) {
47-
int x = rand();
48+
uniform_int_distribution<int> dis(0, 100000);
49+
int x = dis(gen);
4850
if (table.emplace(x).second) {
4951
A.emplace_back(x);
5052
break;
5153
}
5254
}
5355
}
5456
sort(A.begin(), A.end());
55-
int shift = rand() % n;
57+
uniform_int_distribution<int> n_dis(0, n - 1);
58+
int shift = n_dis(gen);
5659
reverse(A.begin(), A.end());
5760
reverse(A.begin(), A.begin() + shift + 1);
5861
reverse(A.begin() + shift + 1, A.end());

Binary_search_circular_array_with_duplicates_template.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
#include <iostream>
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
3+
#include <algorithm>
24
#include <cassert>
5+
#include <iostream>
6+
#include <random>
37
#include <vector>
4-
#include <cstdlib>
5-
#include <ctime>
6-
#include <algorithm>
78

8-
using namespace std;
9+
using std::cout;
10+
using std::default_random_engine;
11+
using std::endl;
12+
using std::random_device;
13+
using std::uniform_int_distribution;
14+
using std::vector;
915

1016
/*
1117
template <typename T>
@@ -46,9 +52,10 @@ int search_smallest(const vector<T> &A) {
4652
return search_smallest_helper(A, 0, A.size() - 1);
4753
}
4854
*/
55+
4956
// @include
5057
template <typename T>
51-
int search_smallest_helper(const vector<T> &A, const int &l, const int &r) {
58+
int search_smallest_helper(const vector<T> &A, int l, int r) {
5259
if (l == r) {
5360
return l;
5461
}
@@ -59,7 +66,7 @@ int search_smallest_helper(const vector<T> &A, const int &l, const int &r) {
5966
} else if (A[m] < A[r]) {
6067
return search_smallest_helper(A, l, m);
6168
} else { // A[m] == A[r]
62-
// Smallest element must exist in either left or right side
69+
// Smallest element must exist in either left or right side.
6370
int l_res = search_smallest_helper(A, l, m);
6471
int r_res = search_smallest_helper(A, m + 1, r);
6572
return A[r_res] < A[l_res] ? r_res : l_res;
@@ -73,20 +80,23 @@ int search_smallest(const vector<T> &A) {
7380
// @exclude
7481

7582
int main(int argc, char *argv[]) {
76-
srand(time(nullptr));
83+
default_random_engine gen((random_device())());
7784
for (int times = 0; times < 10000; ++times) {
7885
int n;
7986
if (argc == 2) {
8087
n = atoi(argv[1]);
8188
} else {
82-
n = 1 + rand() % 10000;
89+
uniform_int_distribution<int> dis(1, 10000);
90+
n = dis(gen);
8391
}
8492
vector<int> A;
8593
for (size_t i = 0; i < n; ++i) {
86-
A.emplace_back(rand());
94+
uniform_int_distribution<int> dis(0, 999999);
95+
A.emplace_back(dis(gen));
8796
}
8897
sort(A.begin(), A.end());
89-
int shift = rand() % n;
98+
uniform_int_distribution<int> n_dis(0, n - 1);
99+
int shift = n_dis(gen);
90100
reverse(A.begin(), A.end());
91101
reverse(A.begin(), A.begin() + shift + 1);
92102
reverse(A.begin() + shift + 1, A.end());

0 commit comments

Comments
 (0)