updated from Atlas repo

This commit is contained in:
Luciano Ramalho
2014-12-14 01:26:42 -02:00
parent dcd59eef31
commit 33d65dc590
21 changed files with 359 additions and 299 deletions

View File

@@ -0,0 +1,38 @@
"""
Arithmetic progression class
>>> ap = ArithmeticProgression(1, .5, 3)
>>> list(ap)
[1.0, 1.5, 2.0, 2.5]
"""
from collections import abc
class ArithmeticProgression:
def __init__(self, begin, step, end):
self.begin = begin
self.step = step
self.end = end
def __iter__(self):
return ArithmeticProgressionIterator(self)
class ArithmeticProgressionIterator(abc.Iterator):
def __init__(self, arithmetic_progression):
self._ap = arithmetic_progression
self._index = 0
def __next__(self):
first = type(self._ap.begin + self._ap.step)(self._ap.begin)
result = first + self._ap.step * self._index
if result < self._ap.end:
self._index += 1
return result
else:
raise StopIteration