1 def my_range_2(start, stop = None, step = 1):
2 if stop == None:
3 if start > 0:
4 start, stop = 0, start
5 else:
6 start, stop = start, 0
7 result = []
8 if step > 0 and start < stop:
9 i = start
10 while (i < stop):
11 result.append(i)
12 i += step
13 elif step < 0 and start > stop:
14 i = start
15 while i > stop:
16 result.append(i)
17 i += step
18 return result
19
20 print(my_range_2(10))
21 print(my_range_2(5, 20, 2))
22 print(my_range_2(1, 10))
23 print(my_range_2(10, 0, -1))
24 print(my_range_2(100, 0, -9))
25 print(my_range_2(10, 20, -5))
26
27 print(my_range_2(100, 0, -10))
28 print(my_range_2(-10))
29 print(my_range_2(-10, -5, 1))
30 print(my_range_2(-10, -20, 1))
31 print(my_range_2(-50, -45))
>>>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 7, 9, 11, 13, 15, 17, 19]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[100, 91, 82, 73, 64, 55, 46, 37, 28, 19, 10, 1]
[]
[100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
[-10, -9, -8, -7, -6]
[]
[-50, -49, -48, -47, -46]
posted on 2013-05-18 14:32
unixfy 阅读(266)
评论(0) 编辑 收藏 引用