-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequences.py
More file actions
204 lines (157 loc) · 6.22 KB
/
sequences.py
File metadata and controls
204 lines (157 loc) · 6.22 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
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
import math
class Sequence:
def __init__(self, function, pos=1):
if not callable(function):
raise TypeError("Sequence requires callable object")
self._function = function
self._pos = pos
def get(self, i):
return self._function(self._pos + i - 1)
def sum(self, n):
return sum(self.limit(n))
def product(self, n):
result = 1
for i in self.limit(n):
result *= i
return result
def product_slice(self, n, m):
result = 1
for i in self[n: m + 1]:
result *= i
return result
def __iter__(self):
while True:
cursor = 1
yield self.get(cursor)
cursor += 1
def limit(self, n):
for i in range(1, n + 1):
yield self.get(i)
def map(self, function):
return Sequence(lambda x: function(self._function(x)), pos=self._pos)
def __neg__(self):
return Sequence(lambda x: -self._function(x), pos=self._pos)
def __pos__(self):
return Sequence(lambda x: +self._function(x), pos=self._pos)
def __add__(self, other):
other_func = lambda x: other._function(x) if not callable(other) else other(x)
return Sequence(lambda x: self._function(x) + other_func(x), pos=self._pos)
def __mul__(self, other):
other_func = lambda x: other._function(x) if not callable(other) else other(x)
return Sequence(lambda x: self._function(x) * other_func(x), pos=self._pos)
def __div__(self, other):
other_func = lambda x: other._function(x) if not callable(other) else other(x)
return Sequence(lambda x: self._function(x) / other_func(x), pos=self._pos)
def __pow__(self, other):
other_func = lambda x: other._function(x) if not callable(other) else other(x)
return Sequence(lambda x: self._function(x) ** other_func(x), pos=self._pos)
def __radd__(self, other):
other_func = lambda x: other._function(x) if not callable(other) else other(x)
return Sequence(lambda x: self._function(x) + other_func(x), pos=self._pos)
def __rmul__(self, other):
other_func = lambda x: other._function(x) if not callable(other) else other(x)
return Sequence(lambda x: self._function(x) * other_func(x), pos=self._pos)
def __rdiv__(self, other):
other_func = lambda x: other._function(x) if not callable(other) else other(x)
return Sequence(lambda x: other / self._function(x), pos=self._pos)
def __lt__(self, other):
raise NotImplementedError
def __le__(self, other):
raise NotImplementedError
def __eq__(self, other):
raise NotImplementedError
def __ne__(self, other):
raise NotImplementedError
def __gt__(self, other):
raise NotImplementedError
def __ge__(self, other):
raise NotImplementedError
def __len__(self):
return float('inf')
def __repr__(self):
return '[{}, {}, {}...]'.format(self[1], self[2], self[3])
def __getitem__(self, item):
if isinstance(item, int):
return self.get(item)
elif isinstance(item, slice):
start = 0 if item.start is None else item.start
end = float("inf") if item.stop is None else item.stop
step = 1 if item.step is None else item.step
if end == float("inf"):
return Sequence(self._function, pos=self._pos)
else:
iterable = []
for i in range(start, end, step):
iterable.append(self.get(i))
return iterable
else:
raise TypeError("Required int or slice, not {}".format(type(item).__name__))
class ArithmethicProgression(Sequence):
def __init__(self, d, a0=0, offset=0):
super().__init__(lambda n: a0 + d * (n - 1), pos=offset)
self._d = d
def sum(self, n):
return ((self[0] + self[n]) / 2) * n
@property
def d(self):
return self._d
def __repr__(self):
return "{" + f"A[d={self.d}]:" + super().__repr__() + '}'
def find_nearest(self, el):
return math.floor((el - self[0] + self.d) / self.d)
def find(self, el):
n = el - self[0] + self.d
if n % self.d == 0:
n = n // self.d
if self[n] == el:
return n
return None
def __contains__(self, key):
return self.find(key) is not None
@classmethod
def from_sequence(cls, iterable):
if len(iterable) > 1:
d = iterable[1] - iterable[0]
a0 = iterable[0]
result = cls(d, a0)
for i, item in enumerate(iterable[2:]):
if result[i] != item:
raise ValueError("Impossible to create ArithmethicProgression from given sequence")
else:
raise ValueError("Not enough information to create ArithmethicProgression")
class GeometricProgression(Sequence):
def __init__(self, q, b0=0, offset=0):
super().__init__(lambda n: b0 * q ** (n - 1), pos=offset)
self._q = q
def sum(self, n):
return self[0] * ((1 - self.q ** n) / (1 - self.q))
def product(self, n, m):
return (self[0] * self[n]) ** (n / 2)
def product_slice(self, n, m):
return self.product(m) / self.product(n - 1)
@property
def q(self):
return self._q
def __repr__(self):
return "{" + f"G[q={self.q}]:" + super().__repr__() + '}'
def find_nearest(self, el):
return round(math.log(el / self[0], self.q) + 1)
def find(self, el):
n = round(math.log(el / self[0], self.q) + 1)
if self[n] == el:
return n
else:
return None
def __contains__(self, key):
return self.find(key) is not None
@classmethod
def from_sequence(cls, iterable):
if len(iterable) > 1:
q = iterable[1] / iterable[0]
a0 = iterable[0]
result = cls(q, a0)
for i, item in enumerate(iterable[2:]):
if result[i] != item:
raise ValueError("Impossible to create GeometricProgression from given sequence")
else:
raise ValueError("Not enough information to create GeometricProgression")