-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumbers.py
263 lines (207 loc) · 7.42 KB
/
numbers.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import math
import random
from .stream import Stream
class NumberStream(Stream):
@staticmethod
def integers():
'''
Returns an infinite stream of integer from 0 to infinity
:return: the infinite stream
'''
return NumberStream(Stream.iterate(0, lambda i: i + 1))
@staticmethod
def odds():
'''
Returns an infinite stream of odds number from 0 to infinity
:return: the infinite stream
'''
return NumberStream(Stream.iterate(1, lambda i: i + 2))
@staticmethod
def evens():
'''
Returns an infinite stream of evens number from 0 to infinity
:return: the infinite stream
'''
return NumberStream(Stream.iterate(0, lambda i: i + 2))
@staticmethod
def primes():
'''
Returns an infinite stream of primes number from 2 to infinity
:return: the infinite stream
'''
def prime_generator():
yield 2
primes = [2]
actual = 1
while True:
actual += 2
for prime in primes:
if actual % prime == 0:
break
else:
primes.append(actual)
yield actual
return NumberStream(prime_generator())
@staticmethod
def randint(lower, upper):
'''
Returns an infinite stream of random integer in range [a, b], including both end points.
:param int lower: min value for random numbers
:param int upper: max value for random numbers
:return: the infinite random stream
'''
return NumberStream(Stream.generate(lambda: random.randint(lower, upper)))
@staticmethod
def random():
'''
Returns an infinite stream of random numbers in range [0, 1], including both end points.
:param int lower: min value for random numbers
:param int upper: max value for random numbers
:return: the infinite random stream
'''
return NumberStream(Stream.generate(random.random))
@staticmethod
def range(start, end, step=1):
'''
Returns a stream of numbers from start to end with specified step.
:param int start: the start value
:param int end: the max value
:param int step: the step
:return: the new stream
'''
return NumberStream(Stream.iterate(start, lambda i: i + step).takeWhile(lambda x: x <= end))
@staticmethod
def pi():
'''
Returns a stream with the digits of PI.
:return: the PI's digits stream
'''
def pi_digits():
"Generate n digits of Pi."
k, a, b, a1, b1 = 2, 4, 1, 12, 4
while True:
p, q, k = k * k, 2 * k + 1, k + 1
a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1
d, d1 = a / b, a1 / b1
while d == d1:
yield int(d)
a, a1 = 10 * (a % b), 10 * (a1 % b1)
d, d1 = a / b, a1 / b1
return NumberStream(Stream(pi_digits()))
def __init__(self, _stream):
if isinstance(_stream, Stream):
self.iterable = _stream.iterable
else:
self.iterable = _stream
def average(self):
'''
Get the average value of the element of the stream.
:return: the average value
'''
_sum = 0
_count = 0
for elem in self:
_sum += elem
_count += 1
return _sum / _count
def takeWhileSmallerThan(self, maximum):
'''
Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller than the specified value
:return: self
'''
return self.takeWhile(lambda x: x < maximum)
def takeWhileSmallerOrEqualThan(self, maximum):
'''
Returns a stream consisting of the longest prefix of elements taken from this stream that is smaller or equal than the specified value
:return: self
'''
return self.takeWhile(lambda x: x <= maximum)
def takeWhileGreaterThan(self, minimum):
'''
Returns a stream consisting of the longest prefix of elements taken from this stream that is greater than the specified value
:return: self
'''
return self.takeWhile(lambda x: x > minimum)
def takeWhileGreaterOrEqualThan(self, minimum):
'''
Returns a stream consisting of the longest prefix of elements taken from this stream that is greater or equal than the specified value
:return: self
'''
return self.takeWhile(lambda x: x >= minimum)
def smallerThan(self, maximum):
'''
Returns a stream consisting of the elements of this stream that are smaller than the specified value
:param int maximum: the maximum value [exclusive]
:return: self
'''
return self.filter(lambda x: x < maximum)
def smallerOrEqualThan(self, maximum):
'''
Returns a stream consisting of the elements of this stream that are smaller or equal than the specified value
:param int maximum: the maximum value [inclusive]
:return: self
'''
return self.filter(lambda x: x <= maximum)
def greaterThan(self, minimum):
'''
Returns a stream consisting of the elements of this stream that are greater than the specified value
:param int minimum: the minimum value [exclusive]
:return: self
'''
return self.filter(lambda x: x > minimum)
def greaterOrEqualThan(self, minimum):
'''
Returns a stream consisting of the elements of this stream that are greater than the specified value
:param int minimum: the minimum value [inclusive]
:return: self
'''
return self.filter(lambda x: x >= minimum)
def multipleOf(self, number):
'''
Returns a stream consisting of the elements of this stream that are multiple of the specified value
:param int number: the value
:return: self
'''
return self.filter(lambda x: x % number == 0)
def square(self):
'''
Returns a stream consisting of the square of the element of this stream
:return: self
'''
return self.pow(2)
def cube(self):
'''
Returns a stream consisting of the cube of the element of this stream
:return: self
'''
return self.pow(3)
def pow(self, power):
'''
Returns a stream consisting of the pow to the specified value of the element of this stream
:return: self
'''
return self.map(lambda x: x ** power)
def sqrt(self):
'''
Returns a stream consisting of the square root of the element of this stream
:return: self
'''
return self.map(math.sqrt)
def log(self):
'''
Returns a stream consisting of the log of the element of this stream
:return: self
'''
return self.map(math.log)
def sin(self):
'''
Returns a stream consisting of the sin of the element of this stream
:return: self
'''
return self.map(math.sin)
def cos(self):
'''
Returns a stream consisting of the cos of the element of this stream
:return: self
'''
return self.map(math.cos)