Created
December 9, 2011 13:10
-
-
Save Djexus/1451464 to your computer and use it in GitHub Desktop.
Python's range... in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import ceil | |
class Range(object): | |
''' | |
Range(start, stop, step) returns the arithmetic progression | |
of numbers, whose lenght is ceil((stop - start) / step) and | |
whose form [start, start + (step * 1), ..., start + (step * | |
(len(obj) - 1)]. | |
Usage: | |
>>> list(Range(0, 10, 3)) | |
[0, 3, 6, 9] | |
>>> list(reversed(Range(0, 10, 3))) | |
[9, 6, 3, 0] | |
''' | |
def __init__(self, start, stop, step): | |
self.start = start | |
self.stop = stop | |
self.step = step | |
def __repr__(self): | |
return 'Range({0}, {1}, {2})'.format(self.start, self.stop, self.step) | |
def __len__(self): | |
return max(0, ceil((self.stop - self.start) / self.step)) | |
def __reversed__(self): | |
return Range(self[-1], (self[0] - self.step), -self.step) | |
def __getitem__(self, index): | |
if not (-len(self) <= index < len(self)): | |
raise IndexError('Index out of the range') | |
return (self.start + (((len(self) + index) % len(self)) * self.step)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment