如何使用递归 运行 下一个列表?
How to run down a list with recursion?
起初,我不得不在没有递归的情况下完成它(只是通过循环,这很容易)。
现在我必须用递归来做,但是我不允许使用任何循环。
我想我必须 运行 使用递归在列表中向下移动,但我不太明白我的基础应该是什么,或者减少...
def long_strings_rec(strings, N):
'''
strings - a list of strings
N - an integer
Returns all strings in 'strings' of length bigger then 'N'
(This function is recursive and does not use loops)
'''
# Your code for question #2 (second function) starts here
# Your code for question #2 (second function) ends here
有什么想法吗?我可以举例说明如何使用递归对列表索引执行操作吗?
我使用辅助函数来做到这一点,就像@7stud 建议的那样:
def helper (strings, K, results):
if len(strings) == 0:
return 0
elif len(strings[0]) > K:
results.append(strings[0])
strings.pop(0)
else:
strings.pop(0)
helper(strings, K, results)
return results
def long_strings_rec (strings, N):
'''
strings - a list of strings
N - an integer
Returns all strings in 'strings' of length bigger then 'N'
(This function is recursive and does not use loops)
'''
# Your code for question #2 (second function) starts here
return helper(strings, N, [])
# Your code for question #2 (second function) ends here
工作得很好。希望没有bug。
下面是如何使用 accumulator
的示例:
def sum(nums):
return helper(nums, 0) #0 is the initial value for the accumulator
def helper(nums, total): #total is the accumulator
if len(nums) == 0:
return total
else:
num = nums.pop()
return helper(nums, total+num)
print sum([1, 2, 3])
--output:--
6
基本上,您重新定义 sum() 以便它接受一个额外的累加器参数变量,然后让 sum() 调用新函数。
看看您是否可以将这些原则应用到您的问题中。
正如比约恩在评论中指出的那样,您也可以这样做:
def mysum(nums, total=0):
if len(nums) == 0:
return total
else:
num = nums.pop()
return sum(nums, total+num)
print mysum([1, 2, 3])
--output:--
6
起初,我不得不在没有递归的情况下完成它(只是通过循环,这很容易)。
现在我必须用递归来做,但是我不允许使用任何循环。
我想我必须 运行 使用递归在列表中向下移动,但我不太明白我的基础应该是什么,或者减少...
def long_strings_rec(strings, N):
'''
strings - a list of strings
N - an integer
Returns all strings in 'strings' of length bigger then 'N'
(This function is recursive and does not use loops)
'''
# Your code for question #2 (second function) starts here
# Your code for question #2 (second function) ends here
有什么想法吗?我可以举例说明如何使用递归对列表索引执行操作吗?
我使用辅助函数来做到这一点,就像@7stud 建议的那样:
def helper (strings, K, results):
if len(strings) == 0:
return 0
elif len(strings[0]) > K:
results.append(strings[0])
strings.pop(0)
else:
strings.pop(0)
helper(strings, K, results)
return results
def long_strings_rec (strings, N):
'''
strings - a list of strings
N - an integer
Returns all strings in 'strings' of length bigger then 'N'
(This function is recursive and does not use loops)
'''
# Your code for question #2 (second function) starts here
return helper(strings, N, [])
# Your code for question #2 (second function) ends here
工作得很好。希望没有bug。
下面是如何使用 accumulator
的示例:
def sum(nums):
return helper(nums, 0) #0 is the initial value for the accumulator
def helper(nums, total): #total is the accumulator
if len(nums) == 0:
return total
else:
num = nums.pop()
return helper(nums, total+num)
print sum([1, 2, 3])
--output:--
6
基本上,您重新定义 sum() 以便它接受一个额外的累加器参数变量,然后让 sum() 调用新函数。
看看您是否可以将这些原则应用到您的问题中。
正如比约恩在评论中指出的那样,您也可以这样做:
def mysum(nums, total=0):
if len(nums) == 0:
return total
else:
num = nums.pop()
return sum(nums, total+num)
print mysum([1, 2, 3])
--output:--
6