使用Python递归地拆分一系列数字
我想用Python编写一个递归函数。我已经写了一个,但它不能正常工作 我有一个从1到1000的数字范围,我想把它分成1到500和501到1000两部分。然后我想递归地这样做,直到每个部分只有20个数字 以下是我的尝试:使用Python递归地拆分一系列数字,python,recursion,Python,Recursion,我想用Python编写一个递归函数。我已经写了一个,但它不能正常工作 我有一个从1到1000的数字范围,我想把它分成1到500和501到1000两部分。然后我想递归地这样做,直到每个部分只有20个数字 以下是我的尝试: mw = range(1,1000) def cuter(mw): if len(mw)<20: return False else: cut=int(len(mw)/2) number.append(mw[0
mw = range(1,1000)
def cuter(mw):
if len(mw)<20:
return False
else:
cut=int(len(mw)/2)
number.append(mw[0])
number.append(cut)
number.append(mw[-1])
return cuter(mw)
cuter(mw)
mw=范围(11000)
def切割机(mw):
如果len(mw)尝试类似的方法,其中seq
是一个包含数字范围的列表:
def cutter(seq):
n = len(seq)
if n <= 20:
# here the recursion stops, do your stuff with the sequence
return
a = cutter(seq[:n/2])
b = cutter(seq[n/2:])
# combine the answer from both subsequences
return
def切割器(seq):
n=长度(序号)
如果你想要一个模糊代码奖…听起来像是家庭作业:\。这是问题的一部分,而不是家庭作业!请帮我解决这个问题,难道你没有比“不起作用”更好的描述你的问题吗?范围(11000)[-20::[1:500],[501:1000][1:250],[250:500],[501:750],[751:1000],。。。。。