-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange_extraction.cpp
67 lines (60 loc) · 2.36 KB
/
range_extraction.cpp
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
#include <string>
#include <vector>
#include <string>
#include <deque>
std::string range_extraction(std::vector<int> args) {
// Handling some cases
if (args.empty()) {
return "";
} else if (args.size() == 1) {
return std::to_string(args[0]);
}
std::string result = "";
std::deque<int> tempQueue; // Double ended queue storing temporarily the elements
int seq = 0; // counting how big is the temporary sequence
bool f = true; // it is true if the element is the first value of the sequence
for (size_t i = 0; i < args.size(); i++) {
if (f) {
tempQueue.push_back(args[i]);
f = false;
}
// If it is the last iteration
if (i == args.size() - 1) {
// Storing the elements in the correct notation
if (args[i] - 1 == args[i - 1]) {
tempQueue.push_back(args[i]);
if (seq >= 2) {
result += std::to_string(tempQueue.front()) + "-" + std::to_string(tempQueue.back()) + ",";
} else if (seq == 1) {
result += std::to_string(tempQueue.front()) + "," + std::to_string(tempQueue.back()) + ",";
}
} else {
result += std::to_string(tempQueue.front()) + ",";
}
// removing the comma(,) character from the back of the string
result = result.substr(0, result.size() - 1);
return result;
}
if (args[i] + 1 == args[i+1]) {
// Incrementing the temporary sequence variable
seq++;
} else {
// Pushing the last element of the temporary sequence in the double ended queue
if (seq >= 1 && args[i] - 1 == args[i - 1]) {
tempQueue.push_back(args[i]);
}
// Storing the elements in the correct notation
if (seq >= 2) {
result += std::to_string(tempQueue.front()) + "-" + std::to_string(tempQueue.back()) + ",";
} else if (seq == 1) {
result += std::to_string(tempQueue.front()) + "," + std::to_string(tempQueue.back()) + ",";
} else {
result += std::to_string(tempQueue.front()) + ",";
}
// Clearing the queue
tempQueue.clear();
seq = 0;
f = true;
}
}
}