-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpick.cpp
More file actions
49 lines (48 loc) · 1.28 KB
/
pick.cpp
File metadata and controls
49 lines (48 loc) · 1.28 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
#include <iostream>
#include <vector>
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Usage: pick num [min [max [suffix]]] < file\nPicks num lines from file such that each line length >= min and length <= max\nwhere zero min picks any line length >= min,\notherwise at least one line has length == min.\n\n");
exit(0);
}
int num = atoi(argv[1]);
int min = 0;
int max = 100000;
const char *suffix = "";
size_t shortest = max;
if (argc >= 3)
min = atoi(argv[2]);
if (argc >= 4)
max = atoi(argv[3]);
if (max < min)
exit(EXIT_FAILURE);
if (argc >= 5)
suffix = argv[4];
std::vector<std::string> patterns;
while (!std::cin.eof())
{
std::string pattern;
getline(std::cin, pattern);
patterns.push_back(pattern);
}
sranddev();
while (num > 0)
{
std::string& pattern = patterns[rand() % patterns.size()];
if ((min <= 0 ? pattern.size() > 0 : pattern.size() >= min) && pattern.size() <= max)
{
if (pattern.size() < shortest)
shortest = pattern.size();
// make sure we have at least one minimal pattern
if (num == 1 && min > 0 && shortest > min)
continue;
// output
std::cout << pattern << suffix << '\n';
// remove from the set
pattern.clear();
--num;
}
}
}