Python:每日一题 98(前40楼答对有奖)
我们知道range作为python的内置函数,可以接收1~3个参数,但与其他函数的缺省参数不同的是它可以省略掉第一个。现在我们自己创建一个函数,模拟range,就叫它range2吧,接收1~3个参数返回生成器,可以这样使用:
for i in range2(...):
其实range本身是个类,如果你创建一个类来实现此功能更好。
公平起见,考虑到VIP可以看到答案,我就不把答案放上来了。
另答题加鱼币限制在40楼以内,望大家快快答题。
def range2(*param):
if len(param)==3:
start,end,step=param
elif len(param)==2:
start,end=param
step=1
else:
end=param
start,step=0,1
while start<end:
yield start
start+=step
for i in range2(10):
print i 本帖最后由 solomonxian 于 2017-9-10 15:10 编辑
感觉类比函数要多写很多啊,
类写得很少,动不动就无限递归
class range2:
def __init__(self, start, stop=None, step=1):
if stop != None:
self.start = start-step
self.stop = stop
else:
self.start = -step
self.stop = start
if step:
self.step = step
else:
raise ValueError("step不能为0")
def _stop(self):
if self.step >0:
if self.start >= self.stop:
raise StopIteration("range2 已经被掏空")
else:
if self.start <= self.stop:
raise StopIteration("range2 已经被掏空")
def __iter__(self):
return self
def __next__(self):
self.start += self.step
self._stop()
return self.start def range2(*params):
if len(params) >= 3:
start,end,step = params
elif len(params) == 2:
start,end = param
step = 1
elif len(params) == 1:
end = params
start,step = 0,1
while start<end:
yield start
start += step
for i in range2(1,5,2,1):
print(i)
def range2(a,b=None,c=None):
if c==None:
c=1
if b==None:
b,a=a,0
x=[]
if c > 0 and b >a:
while b>a:
x.append(a)
a+=c
elif c < 0 and b<a:
while b<a:
x.append(a)
a+=c
else:
return(False)
return(x)
print(range2(1,-10,-3))
不会用类学的不好。。不知道这样 算不算 def range2(*p):
result = []
n = 0
if len(p) == 1:
while n != p:
result.append(n)
n += 1
return result
if len(p) == 2:
while n != p:
result.append(n)
n += 1
return result:n]
if len(p) ==3:
while n != p:
result.append(n)
n += 1
return result:n:p]
else:
print('请输入3个参数')
for i in range2(10):
print(i,end = ' ')
print()
for i in range2(2,10):
print(i,end = ' ')
print()
for i in range2(2,10,2):
print(i,end = ' ')
print() 还没有学到面向对象的知识,只能用笨办法了
def range2(*a):
list0 = []
if(len(a)==1):
start=0
step=1
end=a
elif(len(a)==2):
step=1
start=a
end=a
elif(len(a)==3):
start=a
end=a
step=a
while(start<end):
list0.append(start)
start+=step
return list0
for i in range2(1,5,2):
print(i)
运行结果:
1
3
for i in range2(5):
print(i)
0
1
2
3
4
for i in range2(1,5):
print(i)
1
2
3
4 本帖最后由 wojiaojieer 于 2017-9-13 17:51 编辑
def range2(end,star = 0,step = 1):
while star < end:
yield star
star += step
for i in range2(star = 3,end = 10,step = 3):
print i
还有个小问题:请问下上面的鱼油和冬雪版主,我是个刚入Python 的小鱼儿。我看到楼上的大鱼们写的代码都很多。肯定是有原因的,能简单的说下我这个小鱼没有看出来为什么要写那么长的代码原因吗?感谢~ def range2(*params):
if(len(params) == 3):
start = params
end = params
step = params
elif(len(params) == 2):
start = params
end = params
step = 1
elif (len(params) == 1):
start = 0
end = params
step = 1
else:
print ('param error')
return
while start < end:
yield start
start = start + step
for i in range2(1,5):
print (i)
def range2(*param):
if len(param) == 3:
start,end,step = param
elif len(param) == 2:
step = 1
start,end = param
else:
end= param
start,step = 0,1
while start < end:
yield start
start += step
def range2(*param):
if len(param)==3:
start,end,step=param
elif len(param)==2:
start,end=param
step=1
else:
end=param
start,step=0,1
while start<end:
yield start
start+=step
for i in range2(10):
print i
def range2(*param):
if len(param)==3:
start,end,step=param
elif len(param)==2:
start,end=param
step=1
else:
end=param
start,step=0,1
while start<end:
yield start
start+=step
for i in range2(10):
print i wojiaojieer 发表于 2017-9-13 17:48
def range2(end,star = 0,step = 1):
while star < end:
yield star
你的程序必需要使用者写上参数名,否则range2(1,3)就会出错。 冬雪雪冬 发表于 2017-9-15 16:55
你的程序必需要使用者写上参数名,否则range2(1,3)就会出错。
版主,我也这样考虑过,但我发现直接把函数写成range2(3,1)就直接输出1,2了。所以才有上面的疑问。上面函数定义反了,只要反着写运行就是没有问题的。 class range2:
def __init__(self,start,stop,step=1):
self.start = start
self.stop = stop
self.step = step
self.current = self.start - self.step
def __iter__(self):
return self
def __next__(self):
if self.current >= self.stop:
raise StopIteration()
else:
self.current += self.step
return self.current 里面参数多少的判断是不是和matlab里面的nargin有点像 本帖最后由 776667 于 2017-10-16 10:38 编辑
class range2:
def __init__(self,a,b=None,c=None):
self.gap = c if not c is None else 1
self.start = a-self.gap if not b is None else -1
self.end = b if not b is None else a
def __iter__(self):
return self
def __next__(self):
self.start += self.gap
if ((self.gap < 0) and (self.start <= self.end)) or ((self.gap > 0) and (self.start >= self.end)):
raise StopIteration()
return self.start
楼上有的胖友忽略了第三个参数是负数的情况{:10_330:} #range2 = functools.partial
def range2(num1, num2 = 0, num3 = 1):
if num1 > num2 and num3 > 0:
return Error
elif num1 < num2 and num3 < 0:
return Error
if num1 < num2:
while num1 < num2:
yield num1
num1 += num3
elif num1 > num2 :
while num1 > num2:
yield num1
num1 -= num3
#yield num1
for i in range2(1, 5):
print(i) 本帖最后由 永恒的蓝色梦想 于 2019-8-18 15:36 编辑
def xrange(*var):
for i in var:
if type(i)!=int:raise TypeError(f'{str(type(8))} object cannot be interpreted as an integer')
k=len(var)
if k>3 or k==0:raise TypeError(f'xrange expected 1 arguments, got {k}')
elif k==3:
if var==0:raise ValueError('xrange() arg 3 must not be zero')
start=var;stop=var;step=var
elif k==2:start=var;stop=var;step=1
else:start=0;stop=var;step=1
while start<stop if step>0 else start>stop:yield start;start+=step 看看
页:
[1]