This repository was archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathLesson11.tar
More file actions
executable file
·161 lines (113 loc) · 10 KB
/
Lesson11.tar
File metadata and controls
executable file
·161 lines (113 loc) · 10 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
generator_solution.py 0000664 0001750 0001750 00000001046 13272222456 015536 0 ustar diogenes diogenes import math
def intsum():
a = 0
b = 0
while True:
a += b
yield a
b += 1
def doubler():
a = 1
while True:
yield a
a *= 2
def fib():
a,b = 0,1
while True:
yield b
a, b = b, a+b
def prime():
yield 2 # get this outlier out of the way first
a = 3
while True:
for i in range(2, math.ceil(math.sqrt(a))+1):
if a % i == 0:
break
else:
yield a
a += 2 # skip all even numbers to save some time iterator_1.py 0000664 0001750 0001750 00000001627 13267533161 013674 0 ustar diogenes diogenes #!/usr/bin/env python
"""
Simple iterator examples
"""
class IterateMe_1:
"""
About as simple an iterator as you can get:
returns the sequence of numbers from zero to 4
( like range(4) )
"""
def __init__(self, start=-1, stop=5, step=1):
self.current, self.start = start, start
self.stop = stop
self.step = step
def __iter__(self):
return self
def __next__(self):
self.current += self.step
if self.current < self.stop:
return self.current
else:
raise StopIteration
def reset(self):
self.current = self.start
if __name__ == "__main__":
print("Testing the iterator")
for i in IterateMe_1(-1, 50, 4):
print(i)
it = IterateMe_1(2, 20, 2)
for i in it:
if i > 10:
it.reset()
break
print(i)
for i in it:
print(i)