Skip to content

Commit 639c248

Browse files
TsungHsienLeeTsungHsienLee
authored andcommitted
refactor code
1 parent 3af7794 commit 639c248

32 files changed

+531
-659
lines changed

Anagrams.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
3+
#include <algorithm>
14
#include <iostream>
2-
#include <string>
3-
#include <cstdlib>
4-
#ifdef __clang__
55
#include <unordered_set>
66
#include <unordered_map>
7-
#else
8-
#include <tr1/unordered_set>
9-
#include <tr1/unordered_map>
10-
#endif
11-
#include <algorithm>
7+
#include <random>
8+
#include <string>
9+
#include <utility>
1210
#include <vector>
13-
#include <ctime>
1411

15-
using namespace std;
16-
#ifndef __clang__
17-
using namespace std::tr1;
18-
#endif
12+
using std::cout;
13+
using std::default_random_engine;
14+
using std::endl;
15+
using std::pair;
16+
using std::ostream_iterator;
17+
using std::random_device;
18+
using std::string;
19+
using std::uniform_int_distribution;
20+
using std::unordered_map;
21+
using std::unordered_set;
22+
using std::vector;
1923

2024
// @include
2125
void find_anagrams(const vector<string> &dictionary) {
22-
// Get the sorted string and then insert into hash table
26+
// Get the sorted string and then insert into hash table.
2327
unordered_map<string, vector<string>> hash;
2428
for (const string &s : dictionary) {
2529
string sorted_str(s);
26-
// Use sorted string as the hash code
30+
// Use sorted string as the hash code.
2731
sort(sorted_str.begin(), sorted_str.end());
2832
hash[sorted_str].emplace_back(s);
2933
}
3034

3135
for (const pair<string, vector<string>> &p : hash) {
32-
// Multiple strings with the same hash code => anagrams
36+
// Multiple strings with the same hash code => anagrams.
3337
if (p.second.size() >= 2) {
34-
// Output all strings
38+
// Output all strings.
3539
copy(p.second.begin(), p.second.end(),
3640
ostream_iterator<string>(cout, " "));
3741
cout << endl;
@@ -41,20 +45,24 @@ void find_anagrams(const vector<string> &dictionary) {
4145
// @exclude
4246

4347
string rand_string(int len) {
48+
default_random_engine gen((random_device())());
4449
string ret;
4550
while (len--) {
46-
ret.push_back('a' + rand() % 26);
51+
uniform_int_distribution<int> dis('a', 'z');
52+
ret.push_back(dis(gen));
4753
}
4854
return ret;
4955
}
5056

5157
int main(int argc, char *argv[]) {
52-
srand(time(nullptr));
58+
default_random_engine gen((random_device())());
5359
vector<string> dictionary;
54-
int n = rand() % 100000;
60+
uniform_int_distribution<int> n_dis(0, 99999);
61+
int n = n_dis(gen);
5562
unordered_set<string> table;
5663
for (size_t i = 0; i < n; ++i) {
57-
table.emplace(rand_string(1 + rand() % 12));
64+
uniform_int_distribution<int> dis(1, 12);
65+
table.emplace(rand_string(dis(gen)));
5866
}
5967
for (const string &s : table) {
6068
dictionary.emplace_back(s);

Anonymous_letter.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
#include <iostream>
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
23
#include <algorithm>
34
#include <cassert>
4-
#ifdef __clang__
5-
#include <unordered_map>
6-
#else
7-
#include <tr1/unordered_map>
8-
#endif
5+
#include <iostream>
6+
#include <random>
97
#include <string>
10-
#include <cstdlib>
11-
#include <ctime>
8+
#include <unordered_map>
129

13-
using namespace std;
14-
#ifndef __clang__
15-
using namespace std::tr1;
16-
#endif
10+
using std::boolalpha;
11+
using std::cout;
12+
using std::default_random_engine;
13+
using std::endl;
14+
using std::random_device;
15+
using std::string;
16+
using std::uniform_int_distribution;
17+
using std::unordered_map;
18+
using std::vector;
1719

1820
string rand_string(int len) {
1921
string ret;
22+
default_random_engine gen((random_device())());
2023
while (len--) {
21-
int x = rand() % 27;
24+
uniform_int_distribution<int> dis(0, 26);
25+
int x = dis(gen);
2226
ret += (x < 26) ? 'a' + x : ' ';
2327
}
2428
return ret;
@@ -27,10 +31,10 @@ string rand_string(int len) {
2731
// @include
2832
bool anonymous_letter(const string &L, const string &M) {
2933
unordered_map<char, int> hash;
30-
// Insert all chars in L into a hash table
34+
// Insert all chars in L into a hash table.
3135
for_each(L.begin(), L.end(), [&hash](const char &c) { ++hash[c]; });
3236

33-
// Check chars in M that could cover chars in a hash table
37+
// Check chars in M that could cover chars in a hash table.
3438
for (const char &c : M) {
3539
auto it = hash.find(c);
3640
if (it != hash.cend()) {
@@ -42,18 +46,20 @@ bool anonymous_letter(const string &L, const string &M) {
4246
}
4347
}
4448
}
45-
// No entry in hash means L can be covered by M
49+
// No entry in hash means L can be covered by M.
4650
return hash.empty();
4751
}
4852
// @exclude
4953

5054
int main(int argc, char *argv[]) {
51-
srand(time(nullptr));
55+
default_random_engine gen((random_device())());
5256
string L, M;
5357
if (argc == 3) {
5458
L = argv[1], M = argv[2];
5559
} else {
56-
L = rand_string(1 + rand() % 1000), M = rand_string(1 + rand() % 100000);
60+
uniform_int_distribution<int> L_dis(1, 1000);
61+
uniform_int_distribution<int> M_dis(1, 100000);
62+
L = rand_string(L_dis(gen)), M = rand_string(M_dis(gen));
5763
}
5864
cout << L << endl;
5965
cout << M << endl;

Can_string_be_palindrome.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
2+
3+
#include <cassert>
4+
#include <iostream>
5+
#include <random>
6+
#include <string>
7+
8+
#include "./Can_string_be_palindrome_hash.h"
9+
#include "./Can_string_be_palindrome_sorting.h"
10+
11+
using std::boolalpha;
12+
using std::cout;
13+
using std::default_random_engine;
14+
using std::endl;
15+
using std::random_device;
16+
using std::string;
17+
using std::uniform_int_distribution;
18+
19+
string rand_string(int len) {
20+
string ret;
21+
default_random_engine gen((random_device())());
22+
while (len--) {
23+
uniform_int_distribution<int> dis('a', 'z');
24+
ret += dis(gen);
25+
}
26+
return ret;
27+
}
28+
29+
int main(int argc, char *argv[]) {
30+
default_random_engine gen((random_device())());
31+
for (int times = 0; times < 1000; ++times) {
32+
string s;
33+
if (argc == 2) {
34+
s = argv[1];
35+
} else {
36+
uniform_int_distribution<int> dis(1, 10);
37+
s = rand_string(dis(gen));
38+
}
39+
cout << s << endl;
40+
assert(can_string_be_a_palindrome_hash(s) ==
41+
can_string_be_a_palindrome_sorting(&s));
42+
}
43+
return 0;
44+
}

Can_string_be_palindrome_hash.cpp

Lines changed: 0 additions & 80 deletions
This file was deleted.

Can_string_be_palindrome_hash.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_CAN_STRING_BE_PALINDROME_HASH_H_
4+
#define SOLUTIONS_CAN_STRING_BE_PALINDROME_HASH_H_
5+
6+
#include <string>
7+
#include <unordered_map>
8+
#include <utility>
9+
10+
using std::pair;
11+
using std::string;
12+
using std::unordered_map;
13+
14+
// @include
15+
bool can_string_be_a_palindrome_hash(const string &s) {
16+
unordered_map<char, int> hash;
17+
// Insert each char into hash.
18+
for_each(s.begin(), s.end(), [&hash](const char &c) { ++hash[c]; });
19+
20+
// A string can be permuted as a palindrome if the number of odd time
21+
// chars <= 1.
22+
int odd_count = 0;
23+
for (const pair<char, int> &p : hash) {
24+
if (p.second & 1 && ++odd_count > 1) {
25+
break;
26+
}
27+
}
28+
return odd_count <= 1;
29+
}
30+
// @exclude
31+
#endif // SOLUTIONS_CAN_STRING_BE_PALINDROME_HASH_H_

0 commit comments

Comments
 (0)