-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.py
More file actions
70 lines (51 loc) · 1.29 KB
/
gen.py
File metadata and controls
70 lines (51 loc) · 1.29 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
def new_gen(iterable):
counter = -1
for item in iterable:
if item > 6:
return
counter += 1
if counter % 2 != 0:
continue
else:
yield item
def run_new_gen(iterable):
for item in new_gen(iterable):
print(item)
# run_new_gen([0,1,2,3,4,5,6])
def avg(list):
return sum(list)/len(list)
print(avg(list(x*x for x in range(1,100000) if x <= 3)))
def take(count, iterable):
counter = 0
for item in iterable:
if counter == count:
return
counter += 1
yield item
def run_take_when_needed(items):
count = 0
for item in take(7, items):
if count % 2 == 0:
print(item)
count += 1
def run_take(items):
for item in take(3, items):
print(item)
def distinct(iterable):
seen = set()
for item in iterable:
if item in seen:
continue
yield item
seen.add(item)
def run_distinct(iterable):
for item in distinct(iterable):
print(item)
def run_pipeline(iterable):
for item in take(3, distinct(iterable)):
print(item)
if __name__ == '__main__':
# run_take([1,2,3,4,5])
# run_take([1,2,3,4])
# run_distinct([1,2,1,1,1])
run_pipeline([1,1,1,2,2,2,2,2,2,3,4,5])