-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumOp.cpp
More file actions
133 lines (128 loc) · 2.71 KB
/
Copy pathnumOp.cpp
File metadata and controls
133 lines (128 loc) · 2.71 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
132
133
# include "numOp.h"
// A function that converts a given valid string to an integer
pair <int, bool> numOp :: ctnum(string s)
{
int ans = 0;
bool neg = false;
int i = 0;
if(s[0] == '-')
{
if(s.size() == 1)
return make_pair(-1, false);
neg = true;
i++;
}
else if(s[0] == '+')
{
if(s.size() == 1)
return make_pair(-1, false);
i++;
}
for(; i < (int)s.size(); i++)
{
if(s[i] < 48 || s[i] > 57)
return make_pair(-1, false);
ans = 10 * ans + (s[i] - 48);
}
if(neg)
ans = -1 * ans;
return make_pair(ans, true);
}
// A function that converts a given valid string to a floating point number
pair <float, bool> numOp :: ctf(string s)
{
float ans = 0;
bool neg = false;
int i = 0;
if(s[0] == '-')
{
if(s.size() == 1)
return make_pair(-1, false);
neg = true;
i++;
}
else if(s[0] == '+')
{
if(s.size() == 1)
return make_pair(-1, false);
i++;
}
int ii = i;
while(i < (int)s.size() && s[i] != '.')
{
if(s[i] < 48 || s[i] > 57)
return make_pair(-1, false);
ans = 10 * ans + (s[i] - 48);
i++;
}
if(ii == i)
return make_pair(-1, false);
float temp = 0.1;
i++;
while(i < (int)s.size())
{
if(s[i] < 48 || s[i] > 57)
return make_pair(-1, false);
ans += (temp * (s[i] - 48));
temp /= 10;
i++;
}
if(neg)
ans = -1 * ans;
return make_pair(ans, true);
}
// A function that converts a given integer to a string
string numOp :: nts(int n)
{
if(n == 0)
return "0";
string s;
while(n)
{
s = (char)(n % 10 + '0') + s;
n /= 10;
}
return s;
}
// A function to generate a random integer close to t in the range (0, t].
int numOp :: giveRint(int t)
{
default_random_engine generator(system_clock :: now().time_since_epoch().count());
binomial_distribution <int> distribution(t, 0.995);
int out = distribution(generator);
return out;
}
// A function to generate a set of atmost t integers that sum to almost n
vector <int> numOp :: giveRints(int t, int n, int sz)
{
n = max(n, t);
default_random_engine generator(system_clock :: now().time_since_epoch().count());
if(sz == 0)
{
vector <int> num(t);
int mnum = n / t;
uniform_int_distribution <int> d2(1, mnum);
for(int i = 0; i < t; i++)
num[i] = d2(generator);
return num;
}
else
{
if(t == 1)
{
vector <int> num;
num.push_back(n);
return num;
}
array <float, 3> intervals {1.0, max(0.15 * t, 1.0), 1.0 * t};
array <float, 3> weights {30.0, 20.0, 0.5};
piecewise_linear_distribution <float> distribution(intervals.begin(), intervals.end(), weights.begin());
int times = distribution(generator);
int mnum = n / times;
vector <int> num(times);
binomial_distribution <int> d2(mnum, 0.99);
for(int i = 0; i < times; i++)
num[i] = max(1ll, d2(generator) % (mnum + 1));
return num;
}
}