-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange-extraction.cpp
More file actions
54 lines (52 loc) · 971 Bytes
/
Copy pathrange-extraction.cpp
File metadata and controls
54 lines (52 loc) · 971 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>
#include <cmath>
using namespace std;
bool is_interval(vector<int> &args, int indice, int &size)
{
if (indice >= size - 2)
{
return false;
}
if (args[indice] + 1 == args[indice + 1] && args[indice] + 2 == args[indice + 2])
{
return true;
}
return false;
}
std::string range_extraction(std::vector<int> args)
{
int size = args.size();
string result = "";
for (int i = 0; i < size; i++)
{
if (is_interval(args, i, size))
{
if (i != 0)
{
result += ",";
}
result += to_string(args[i]);
while ((i < size - 1) && (args[i] + 1 == args[i + 1]))
{
i++;
}
result += "-" + to_string(args[i]);
}
else
{
if (i == 0)
{
result += to_string(args[i]);
}
else
{
result += "," + to_string(args[i]);
}
}
}
return result;
}