updated from Atlas repo
This commit is contained in:
@@ -1,32 +1,31 @@
|
||||
"""
|
||||
Arithmetic progression class
|
||||
Arithmetic progression generator function::
|
||||
|
||||
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||
>>> ap = aritprog_gen(1, .5, 3)
|
||||
>>> list(ap)
|
||||
[1.0, 1.5, 2.0, 2.5]
|
||||
|
||||
>>> ap = aritprog_gen(0, 1/3, 1)
|
||||
>>> list(ap)
|
||||
[0.0, 0.3333333333333333, 0.6666666666666666]
|
||||
>>> from fractions import Fraction
|
||||
>>> ap = aritprog_gen(0, Fraction(1, 3), 1)
|
||||
>>> list(ap)
|
||||
[Fraction(0, 1), Fraction(1, 3), Fraction(2, 3)]
|
||||
>>> from decimal import Decimal
|
||||
>>> ap = aritprog_gen(0, Decimal('.1'), .3)
|
||||
>>> list(ap)
|
||||
[Decimal('0.0'), Decimal('0.1'), Decimal('0.2')]
|
||||
|
||||
"""
|
||||
|
||||
import array
|
||||
from collections import abc
|
||||
|
||||
class ArithmeticProgression:
|
||||
|
||||
def __init__(self, begin, step, end):
|
||||
self.begin = begin
|
||||
self.step = step
|
||||
self.end = end
|
||||
self._build()
|
||||
|
||||
def _build(self):
|
||||
self._numbers = array.array('d')
|
||||
n = self.begin
|
||||
while n < self.end:
|
||||
self._numbers.append(n)
|
||||
n += self.step
|
||||
|
||||
def __iter__(self):
|
||||
for item in self._numbers:
|
||||
yield item
|
||||
return StopIteration
|
||||
# BEGIN ARITPROG_GENFUNC
|
||||
def aritprog_gen(begin, step, end=None):
|
||||
result = type(begin + step)(begin)
|
||||
forever = end is None
|
||||
index = 0
|
||||
while forever or result < end:
|
||||
yield result
|
||||
index += 1
|
||||
result = begin + step * index
|
||||
# END ARITPROG_GENFUNC
|
||||
|
||||
Reference in New Issue
Block a user