-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAmazon.cpp
More file actions
131 lines (114 loc) · 3.91 KB
/
Copy pathAmazon.cpp
File metadata and controls
131 lines (114 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//Copyright [2018] [Haibo Yan]
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//
// Created by Haibo Yan on 3/31/18.
//
// see https://github.com/billyean/algo_system_design/blob/master/amazon_1.png
// and https://github.com/billyean/algo_system_design/blob/master/amazon_2.png
#include "Amazon.h"
#include <map>
#include <regex>
#include <set>
#include <iostream>
#include <algorithm>
vector<string> Amazon::retrieveMostFrequentlyUsedWords(string literatureText, vector<string> wordsToExclude) {
set<string> exclude;
for (auto iw = wordsToExclude.begin(); iw != wordsToExclude.end(); iw++) {
string w = (*iw);
transform(w.begin(), w.end(), w.begin(), ::tolower);
exclude.insert(w);
}
map<string, int> words;
regex r("([A-Za-z]+)");
sregex_iterator next(literatureText.begin(), literatureText.end(), r), end;
while (next != end) {
string w = next->str();
transform(w.begin(), w.end(), w.begin(), ::tolower);
if (find(exclude.begin(), exclude.end(), w) == exclude.end()) {
if (words.find(w) == words.end()) {
words.insert({w, 1});
} else {
words[w]++;
}
}
next++;
}
int top = 0;
for (auto iw = words.begin(); iw != words.end(); iw++) {
top = max(top, iw->second);
}
vector<string> mostFrequently;
for (auto iw = words.begin(); iw != words.end(); iw++) {
if (iw->second == top) {
mostFrequently.push_back(iw->first);
}
}
sort(mostFrequently.begin(), mostFrequently.end());
return mostFrequently;
}
vector<string> Amazon::reorderLines(int logFileSize, vector<string> logLines) {
vector<string> words, integers;
cout << endl;
for (auto line : logLines) {
regex r("([a-zA-Z]+[0-9]+)( [0-9]+)(.*)");
if (regex_match(line, r)) {
integers.push_back(line);
} else {
words.push_back(line);
}
}
sort(words.begin(), words.end(), [](string& s1, string& s2) {
regex r("^([A-Za-z]+)([0-9]+) (.+)");
smatch m1, m2;
bool b1 = regex_search(s1, m1, r);
bool b2 = regex_search(s2, m2, r);
if (b1 && b2) {
if (m1[3] == m2[3]) {
auto identifier_str1 = m1[1];
auto identifier_str2 = m2[1];
auto i1 = stoi(m1[2]);
auto i2 = stoi(m2[2]);
if (i1 == i2) {
return identifier_str1 < identifier_str2;
}
return i1 < i2;
} else {
return m1[3] < m2[3];
}
} else {
return false;
}
});
// sort(integers.begin(), integers.end(), [](string& s1, string& s2) {
// regex r("^([A-Za-z]+)([0-9]+)");
// smatch m1, m2;
// bool b1 = regex_search(s1, m1, r);
// bool b2 = regex_search(s2, m2, r);
// if (b1 && b2) {
// auto identifier_str1 = m1[1];
// auto identifier_str2 = m2[1];
// auto i1 = stoi(m1[2]);
// auto i2 = stoi(m2[2]);
// cout << "i1 = " << i1 << "; i2 = " << i2 << endl;
// if (i1 == i2) {
// return identifier_str1 < identifier_str2;
// }
// return i1 < i2;
// } else {
// return false;
// }
// });
words.insert(words.end(), integers.begin(), integers.end());
return words;
}