Skip to content

Commit 219af6b

Browse files
TsungHsienLeeTsungHsienLee
TsungHsienLee
authored and
TsungHsienLee
committed
Refactoring code
1 parent b049f95 commit 219af6b

Some content is hidden

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

48 files changed

+900
-959
lines changed

0-sum_subset.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
#include <iostream>
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
23
#include <algorithm>
3-
#include <numeric>
44
#include <cassert>
5+
#include <iostream>
6+
#include <numeric>
7+
#include <random>
58
#include <vector>
6-
#include <cstdlib>
7-
#include <ctime>
89

9-
using namespace std;
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::vector;
1016

1117
// @include
1218
vector<int> find_0_sum_subset(const vector<int> &A) {
@@ -30,12 +36,12 @@ vector<int> find_0_sum_subset(const vector<int> &A) {
3036
table[prefix_sum[i]] = i;
3137
}
3238
// @exclude
33-
return {}; // it should not happen
39+
return {}; // it should not happen
3440
// @include
3541
}
3642
// @exclude
3743

38-
void check_ans(vector<int> &A, vector<int> &ans) {
44+
void check_ans(const vector<int> &A, const vector<int> &ans) {
3945
int sum = 0;
4046
for (const int &a : ans) {
4147
sum = (sum + A[a]) % A.size();
@@ -44,29 +50,30 @@ void check_ans(vector<int> &A, vector<int> &ans) {
4450
}
4551

4652
int main(int argc, char *argv[]) {
47-
//srand(time(nullptr));
53+
default_random_engine gen((random_device())());
4854
for (int times = 0; times < 1000; ++times) {
4955
int n;
5056
vector<int> A;
5157
if (argc == 2) {
5258
n = atoi(argv[1]);
5359
A.resize(n);
60+
uniform_int_distribution<int> dis(0, 9999);
5461
for (int i = 0; i < n; ++i) {
55-
A[i] = rand() % 10000;
62+
A[i] = dis(gen);
5663
}
5764
} else if (argc > 2) {
5865
for (int i = 1; i < argc; ++i) {
5966
A.emplace_back(atoi(argv[i]));
6067
}
6168
} else {
62-
n = 1 + rand() % 100;
69+
uniform_int_distribution<int> n_dis(1, 100);
70+
n = n_dis(gen);
6371
A.resize(n);
72+
uniform_int_distribution<int> dis(0, 9999);
6473
for (int i = 0; i < n; ++i) {
65-
A[i] = rand() % 10000;
74+
A[i] = dis(gen);
6675
}
6776
}
68-
//copy(A.begin(), A.end(), ostream_iterator<int>(cout, " "));
69-
//cout << endl;
7077
vector<int> ans = find_0_sum_subset(A);
7178
check_ans(A, ans);
7279
}

Bignumber_multiplication.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
#include <algorithm>
44
#include <cassert>
5-
#include <cstring>
65
#include <iostream>
76
#include <random>
87
#include <string>
98
#include <vector>
109

10+
#include "./execute-shell.h"
11+
1112
using std::cout;
1213
using std::default_random_engine;
1314
using std::endl;
@@ -20,7 +21,7 @@ using std::vector;
2021
// @include
2122
class BigInt {
2223
public:
23-
explicit BigInt(const int &capacity) : sign_(1), digits_(capacity) {}
24+
explicit BigInt(int capacity) : sign_(1), digits_(capacity) {}
2425

2526
explicit BigInt(const string &s) : sign_(s[0] == '-' ? -1 : 1),
2627
digits_(s.size() - (s[0] == '-')) {
@@ -40,18 +41,18 @@ class BigInt {
4041
int carry = 0;
4142
for (j = 0; j < digits_.size() || carry; ++j) {
4243
int n_digit = result.digits_[i + j] +
43-
(j < digits_.size() ? n.digits_[i] * digits_[j] : 0) +
44-
carry;
44+
(j < digits_.size() ? n.digits_[i] * digits_[j] : 0) +
45+
carry;
4546
result.digits_[i + j] = n_digit % 10;
4647
carry = n_digit / 10;
4748
}
4849
}
4950
}
5051

51-
// If one number is 0, the result size should be 0
52+
// If one number is 0, the result size should be 0.
5253
if ((digits_.size() == 1 && digits_.front() == 0) ||
53-
(n.digits_.size() == 1 && n.digits_.front() == 0)) {
54-
result.digits_.resize(1);
54+
(n.digits_.size() == 1 && n.digits_.front() == 0)) {
55+
result.sign_ = 1, result.digits_.resize(1);
5556
} else {
5657
result.digits_.resize(i + j - 1);
5758
}
@@ -97,18 +98,28 @@ string rand_string(int len) {
9798
return ret;
9899
}
99100

100-
int main(int argc, char *argv[]) {
101+
void simple_test() {
101102
assert((BigInt("0") * BigInt("1000")).toString() == "0");
102103
assert((BigInt("131412") * BigInt("-1313332")).toString() == "-172587584784");
103-
string s1, s2;
104-
if (argc == 3) {
105-
s1 = argv[1], s2 = argv[2];
106-
} else {
107-
default_random_engine gen((random_device())());
108-
uniform_int_distribution<int> dis(0, 19);
109-
s1 = rand_string(dis(gen)), s2 = rand_string(dis(gen));
104+
}
105+
106+
int main(int argc, char *argv[]) {
107+
simple_test();
108+
for (int times = 0; times < 1000; ++times) {
109+
string s1, s2;
110+
if (argc == 3) {
111+
s1 = argv[1], s2 = argv[2];
112+
} else {
113+
default_random_engine gen((random_device())());
114+
uniform_int_distribution<int> dis(0, 19);
115+
s1 = rand_string(dis(gen)), s2 = rand_string(dis(gen));
116+
}
117+
BigInt res = BigInt(s1) * BigInt(s2);
118+
cout << s1 << " * " << s2 << " = " << res.toString() << endl;
119+
string command = "bc <<<" + s1 + "*" + s2;
120+
string result = execute_shell(command);
121+
cout << "answer = " << result;
122+
assert(res.toString().compare(result.substr(0, result.size() - 1)) == 0);
110123
}
111-
BigInt res = BigInt(s1) * BigInt(s2);
112-
cout << s1 << " * " << s2 << " = " << res.toString() << endl;
113124
return 0;
114125
}

Division.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
#include <iostream>
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
23
#include <cassert>
3-
#include <ctime>
4-
#include <climits>
5-
#include <cstdlib>
4+
#include <iostream>
5+
#include <limits>
6+
#include <random>
67

7-
using namespace std;
8+
using std::cout;
9+
using std::default_random_engine;
10+
using std::endl;
11+
using std::numeric_limits;
12+
using std::random_device;
13+
using std::uniform_int_distribution;
814

9-
unsigned divide_x_y_bsearch(const unsigned &x, const unsigned &y) {
15+
unsigned divide_x_y_bsearch(unsigned x, unsigned y) {
1016
if (x < y) {
1117
return 0;
1218
}
1319

1420
int power_left = 0;
15-
int power_right = sizeof(unsigned) << 3;
21+
int power_right = sizeof(unsigned) << 3;
1622
int power_mid = -1;
1723
while (power_left < power_right) {
1824
int tmp = power_mid;
@@ -39,7 +45,7 @@ unsigned divide_x_y_bsearch(const unsigned &x, const unsigned &y) {
3945
}
4046

4147
// @include
42-
unsigned divide_x_y(const unsigned &x, const unsigned &y) {
48+
unsigned divide_x_y(unsigned x, unsigned y) {
4349
if (x < y) {
4450
return 0;
4551
}
@@ -53,10 +59,7 @@ unsigned divide_x_y(const unsigned &x, const unsigned &y) {
5359
}
5460
// @exclude
5561

56-
57-
void foo() { }
58-
59-
void simple_test(void) {
62+
void simple_test() {
6063
assert(divide_x_y(64, 1) == 64);
6164
assert(divide_x_y(64, 2) == 32);
6265
assert(divide_x_y(64, 3) == 21);
@@ -77,19 +80,20 @@ void simple_test(void) {
7780

7881
int main(int argc, char *argv[]) {
7982
simple_test();
80-
int MAX = UINT_MAX;
81-
//srand(time(nullptr));
8283
if (argc == 3) {
8384
unsigned x = atoi(argv[1]), y = atoi(argv[2]);
8485
assert(x / y == divide_x_y(x, y));
8586
assert(x / y == divide_x_y_bsearch(x, y));
8687
} else {
87-
for (int times = 0; times < 100000000; ++times) {
88-
unsigned x = rand() % MAX;
89-
unsigned y = rand() % MAX;
90-
y = (y==0) ? 1 : y; // ensure no divide by 0
91-
assert((x/y) == divide_x_y(x, y));
92-
assert( (x/y) == divide_x_y_bsearch(x, y));
88+
default_random_engine gen((random_device())());
89+
uniform_int_distribution<unsigned> dis(0, numeric_limits<int>::max());
90+
for (int times = 0; times < 100000; ++times) {
91+
unsigned x = dis(gen), y = dis(gen);
92+
y = (y == 0) ? 1 : y; // ensure no divide by 0
93+
cout << "times = " << times << ", x = " << x << ", y = " << y << endl;
94+
cout << "first = " << x / y << ", second = " << divide_x_y(x, y) << endl;
95+
assert(x / y == divide_x_y(x, y));
96+
assert(x / y == divide_x_y_bsearch(x, y));
9397
}
9498
}
9599
return 0;

Dutch_national_flag.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@ using std::vector;
1616

1717
// @include
1818
template <typename T>
19-
void dutch_flag_partition(vector<T> &A, const int &pivot_index) {
20-
T pivot = A[pivot_index];
19+
void dutch_flag_partition(vector<T>* A, int pivot_index) {
20+
T pivot = (*A)[pivot_index];
2121
/**
2222
* Keep the following invariants during partitioning:
23-
* bottom group: A[0 : smaller - 1]
24-
* middle group: A[smaller : equal - 1]
25-
* unclassified group: A[equal : larger]
26-
* top group: A[larger + 1 : A.size() - 1]
23+
* bottom group: (*A)[0 : smaller - 1]
24+
* middle group: (*A)[smaller : equal - 1]
25+
* unclassified group: (*A)[equal : larger]
26+
* top group: (*A)[larger + 1 : A->size() - 1]
2727
*/
28-
int smaller = 0, equal = 0, larger = A.size() - 1;
28+
int smaller = 0, equal = 0, larger = A->size() - 1;
2929
// When there is any unclassified element
3030
while (equal <= larger) {
31-
// A[equal] is the incoming unclassified element
32-
if (A[equal] < pivot) {
33-
swap(A[smaller++], A[equal++]);
34-
} else if (A[equal] == pivot) {
31+
// (*A)[equal] is the incoming unclassified element
32+
if ((*A)[equal] < pivot) {
33+
swap((*A)[smaller++], (*A)[equal++]);
34+
} else if ((*A)[equal] == pivot) {
3535
++equal;
36-
} else { // A[equal] > pivot
37-
swap(A[equal], A[larger--]);
36+
} else { // (*A)[equal] > pivot
37+
swap((*A)[equal], (*A)[larger--]);
3838
}
3939
}
4040
}
@@ -63,7 +63,7 @@ int main(int argc, char *argv[]) {
6363
uniform_int_distribution<int> dis(0, A.size() - 1);
6464
int pivot_index = dis(gen);
6565
int pivot = A[pivot_index];
66-
dutch_flag_partition(A, pivot_index);
66+
dutch_flag_partition(&A, pivot_index);
6767
int i = 0;
6868
while (A[i] < pivot && i < A.size()) {
6969
cout << A[i] << ' ';

Equiv_classes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
22

3+
#include <algorithm>
34
#include <cassert>
45
#include <iostream>
56
#include <iterator>
@@ -31,7 +32,7 @@ int backtrace(const vector<int> &F, int idx) {
3132
* F of length N; F[i] holds the smallest index of all the elements that
3233
* i is equivalent to.
3334
*/
34-
vector<int> compute_equival_classes(const int &n, const vector<int> &A,
35+
vector<int> compute_equival_classes(int n, const vector<int> &A,
3536
const vector<int> &B) {
3637
// Each element maps to itself
3738
vector<int> F(n);

GCD.cpp

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,18 @@
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
13
#include <iostream>
24
#include <cassert>
5+
#include <limits>
6+
#include <random>
37

4-
using namespace std;
5-
6-
// @include
7-
long long GCD(const long long &x, const long long &y) {
8-
if (x == 0) {
9-
return y;
10-
} else if (y == 0) {
11-
return x;
12-
} else if (!(x & 1) && !(y & 1)) { // x and y are even integers
13-
return GCD(x >> 1, y >> 1) << 1;
14-
} else if (!(x & 1) && y & 1) { // x is even integer, and y is odd integer
15-
return GCD(x >> 1, y);
16-
} else if (x & 1 && !(y & 1)) { // x is odd integer, and y is even integer
17-
return GCD(x, y >> 1);
18-
} else if (x > y) { // both x and y are odd integers, and x > y
19-
return GCD(x - y, y);
20-
}
21-
return GCD(x, y - x); // both x and y are odd integers, and x <= y
22-
}
8+
#include "./GCD.h"
239

24-
long long another_GCD(long long a, long long b) {
25-
if (b) {
26-
while ((a %= b) && (b %= a));
27-
}
28-
return a + b;
29-
}
30-
// @exclude
10+
using std::cout;
11+
using std::default_random_engine;
12+
using std::endl;
13+
using std::numeric_limits;
14+
using std::random_device;
15+
using std::uniform_int_distribution;
3116

3217
int main(int argc, char *argv[]) {
3318
long long x = 18, y = 12;
@@ -37,9 +22,11 @@ int main(int argc, char *argv[]) {
3722
cout << GCD(x, y) << endl;
3823
assert(GCD(x, y) == another_GCD(x, y));
3924
} else {
40-
srand(time(nullptr));
25+
default_random_engine gen((random_device())());
4126
for (int times = 0; times < 1000; ++times) {
42-
x = rand(), y = rand();
27+
uniform_int_distribution<long long>
28+
dis(1, numeric_limits<long long>::max());
29+
x = dis(gen), y = dis(gen);
4330
assert(GCD(x, y) == another_GCD(x, y));
4431
}
4532
}

GCD.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
3+
#ifndef SOLUTIONS_GCD_H_
4+
#define SOLUTIONS_GCD_H_
5+
6+
// @include
7+
long long GCD(long long x, long long y) {
8+
if (x == 0) {
9+
return y;
10+
} else if (y == 0) {
11+
return x;
12+
} else if (!(x & 1) && !(y & 1)) { // x and y are even integers
13+
return GCD(x >> 1, y >> 1) << 1;
14+
} else if (!(x & 1) && y & 1) { // x is even integer, and y is odd integer
15+
return GCD(x >> 1, y);
16+
} else if (x & 1 && !(y & 1)) { // x is odd integer, and y is even integer
17+
return GCD(x, y >> 1);
18+
} else if (x > y) { // both x and y are odd integers, and x > y
19+
return GCD(x - y, y);
20+
}
21+
return GCD(x, y - x); // both x and y are odd integers, and x <= y
22+
}
23+
// @exclude
24+
25+
long long another_GCD(long long a, long long b) {
26+
if (b) {
27+
while ((a %= b) && (b %= a));
28+
}
29+
return a + b;
30+
}
31+
#endif // SOLUTIONS_GCD_H_

0 commit comments

Comments
 (0)