-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw_8.py
More file actions
74 lines (53 loc) · 1.13 KB
/
hw_8.py
File metadata and controls
74 lines (53 loc) · 1.13 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
import random
def random_gen(k = 100):
while True:
yield random.randint(0, k)
x = random_gen()
print(next(x))
print(next(x))
def range_gen(start, stop, step = 1):
i = start
while i < stop:
yield i
i += step
x = range_gen(10, 20)
print(list(x))
x = range_gen(1, 100, 20)
print(list(x))
def map_gen(func, lst):
i = 0
l = len(lst)
while i < l:
yield func(lst[i])
i += 1
x = map_gen(lambda x: x**2, [1, 2, 3])
print(list(x))
x = map(lambda x: x**2, [1, 2, 3])
print(list(x))
def enum_gen(lst, begin_index = 0):
i = begin_index
l = len(lst)
c = 0
while c < l:
yield i, lst[c]
i += 1
c += 1
x = enum_gen([1, 2, 3], 5)
print(list(x))
x = enumerate([1, 2, 3], 5)
print(list(x))
def zip_gen(lst1, lst2):
i1 = 0
i2 = 0
l1 = len(lst1)
l2 = len(lst2)
while i1 < l1 or i2 < l2:
item1 = lst1[i1] if i1 < l1 else None
item2 = lst2[i2] if i2 < l2 else None
yield item1, item2
i1 += 1
i2 += 1
x = zip_gen([1, 2, 3], ["a", "b"])
print(list(x))
x = zip([1, 2], ["a", "b", "c"])
print(list(x))