-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
144 lines (134 loc) · 4.73 KB
/
main.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <random>
#include <chrono>
#include <algorithm>
#include <utility>
#include <cstring>
using namespace std;
using uint = unsigned int;
std::default_random_engine random_engine(std::chrono::system_clock::now().time_since_epoch().count());
uint get_random(const std::set< uint > excluded, uint max_n)
{
while (true) {
uint value = random_engine() % max_n;
if (excluded.find(value) == excluded.end()) {
return value;
}
}
}
void fill_vector(std::vector< uint >& vec, uint size, std::map< uint, float > frequencies, uint max_n)
{
vec.clear();
vec.reserve(size);
std::set< uint > excluded;
for (auto pair : frequencies) {
uint value = pair.first;
uint count = uint(size * pair.second);
for (uint i = 0; i < count; i++) {
vec.push_back(value);
}
excluded.insert(value);
}
uint rest = size - vec.size();
for (uint i = 0; i < rest; i++) {
vec.push_back(get_random(excluded, max_n));
}
vec.resize(size);
std::shuffle(vec.begin(), vec.end(), random_engine);
}
void print_vector(const char* prefix, const std::vector< uint > vec)
{
std::cout << prefix;
for (auto value : vec) {
std::cout << " " << value;
}
std::cout << std::endl;
}
uint find_number(const std::vector< uint >& vec, uint m)
{
std::map< uint, int > most_frequent_numbers;
uint index = 0;
for (auto value : vec) {
if (index != 0) {
for (auto pos = most_frequent_numbers.begin(); pos != most_frequent_numbers.end(); ++pos) {
pos->second -= 1;
}
}
uint diff = m + 1;
uint buffer_size = m + 1;
if (most_frequent_numbers.find(value) != most_frequent_numbers.end()) {
// Increase counter of existing value.
most_frequent_numbers[value]+=diff;
} else if (uint(most_frequent_numbers.size()) < buffer_size) {
// Add new item to the table. Frequency is 1.
most_frequent_numbers.insert(std::make_pair(value, diff));
} else {
// Replace the least frequent item in the table.
auto pos = std::min_element(most_frequent_numbers.begin(), most_frequent_numbers.end(), [](std::pair< uint, int > p1, std::pair< uint, int > p2) {
return p1.second < p2.second;
});
most_frequent_numbers.erase(pos);
most_frequent_numbers.insert(std::make_pair(value, diff));
}
index++;
}
for (auto pos = most_frequent_numbers.begin(); pos != most_frequent_numbers.end(); ++pos) {
pos->second = 0;
}
for (auto value : vec) {
if (most_frequent_numbers.find(value) != most_frequent_numbers.end()) {
most_frequent_numbers[value]++;
}
}
auto pos = std::max_element(most_frequent_numbers.begin(), most_frequent_numbers.end(), [](std::pair< uint, int > p1, std::pair< uint, int > p2) {
return p1.second < p2.second;
});
return pos->first;
}
float get_frequency(const std::vector< uint >& vec, uint value)
{
uint count = 0;
for (auto v : vec) {
if (v == value) {
count++;
}
}
return float(count) / vec.size();
}
int main(int argc, char** argv)
{
if (argc == 2 && std::strcmp(argv[1], "-c") == 0) {
std::vector< uint > input;
fill_vector(input, 30, std::map< uint, float >{ { 1, 0.34 } }, 10);
print_vector("INPUT ", input);
std::cout << "RESULT: " << find_number(input, 3) << std::endl;
} else if (argc == 2 && std::strcmp(argv[1], "-t") == 0) {
constexpr uint NUM_TESTS = 100000;
for (uint max_n = 4; max_n <= 30; max_n++) {
uint success = 0;
uint fail = 0;
for (uint i = 0; i < NUM_TESTS; i++) {
std::vector< uint > input;
fill_vector(input, 450, std::map< uint, float >{ { 1, 0.34 } }, max_n);
uint value = find_number(input, 3);
float frequency = get_frequency(input, value);
if (frequency >= float(1) / 3) {
success++;
} else {
std::cout << "========================\n";
print_vector("INPUT = ", input);
std::cout << "Output " << value << "\n";
std::cout << "Frequency " << frequency << "\n";
std::cout << "========================\n";
fail++;
return 0;
}
}
std::cout << "#" << max_n << "Number of tests: " << NUM_TESTS << ", success = " << success << ", fail = " << fail << std::endl;
}
}
return 0;
}