-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerators_class_and_function.py
More file actions
65 lines (53 loc) · 1.27 KB
/
generators_class_and_function.py
File metadata and controls
65 lines (53 loc) · 1.27 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
def generate_string(string, frequency):
for char in string:
for _ in range(frequency):
yield char
class GenerateString:
def __init__(self, string, frequency):
self.string = string
self.frequency = frequency
def __iter__(self):
self.current_char_index = 0
self.char_counter = 0
return self
def __next__(self):
if self.char_counter >= self.frequency:
self.char_counter = 0
self.current_char_index += 1
if self.current_char_index >= len(self.string):
raise StopIteration
self.char_counter += 1
return self.string[self.current_char_index]
res = GenerateString("hello", 3)
itr = iter(res)
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
print(next(itr))
res2 = generate_string("hello", 3)
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))
print(next(res2))