-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyRange.py
49 lines (30 loc) · 974 Bytes
/
myRange.py
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
class MyRange:
step=1
stop=0
start=0
def __init__(self,stop,start=0,step=1):
self.start=start
self.stop=stop
self.step=step
if self.step !=1 or self.start !=0:
self.start,self.stop = self.stop,self.start
def __iter__(self):
if not self.valid():
raise BaseException("Invalid interval!")
return
return self.__next__()
def __next__(self):
s = self.start
if self.start <= self.stop:
while s < self.stop:
yield s
s += self.step
else:
while s > self.stop:
yield s
s += self.step
def valid(self):
if (self.step==0) or (self.start > self.stop and self.step >= 0) or (self.start < self.stop and self.step <=0):
return False
else:
return True