sync to Atlas repo
This commit is contained in:
29
iterables/aritprog_v4.py
Normal file
29
iterables/aritprog_v4.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""
|
||||
Arithmetic progression class
|
||||
|
||||
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||
>>> list(ap)
|
||||
[1.0, 1.5, 2.0, 2.5]
|
||||
|
||||
|
||||
"""
|
||||
|
||||
import array
|
||||
from collections import abc
|
||||
|
||||
class ArithmeticProgression:
|
||||
|
||||
def __init__(self, begin, step, end=None):
|
||||
self.begin = begin
|
||||
self.step = step
|
||||
self.end = end # None -> "infinite" series
|
||||
|
||||
def __iter__(self):
|
||||
result = type(self.step)(self.begin)
|
||||
forever = self.end is None
|
||||
index = 0
|
||||
while forever or result < self.end:
|
||||
yield result
|
||||
index += 1
|
||||
result = self.begin + self.step * index
|
||||
raise StopIteration
|
||||
Reference in New Issue
Block a user