-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
48 lines (41 loc) · 909 Bytes
/
example.cpp
File metadata and controls
48 lines (41 loc) · 909 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
/*
Let we want to implement Python generator like this:
def StangeGenerator(seed):
if seed > 228:
yield 42
i = 1
while ((seed + i) % 42 != (seed - i) % 228):
yield seed // i
i += 1
else:
yield -1
We can do it!
*/
#include <iostream>
#include "base.h"
class StrangeGenerator : public Generator<int>
{
int seed, i = 0;
GENERATE
if (seed > 228) {
YIELD(42);
i = 1;
while ((seed + i) % 42 != (seed - i) % 22)
{
YIELD(seed / i);
i += 1;
}
}
else {
YIELD(-1);
}
TERMINATE
public:
explicit StrangeGenerator(int seed) : seed(seed) {}
};
using namespace std;
int main() {
for (auto e : StrangeGenerator(2000)) {
cout << e << endl;
}
}