question archive Please explain how I would write a recursive function num_pairs( L, n ) whose inputs are L, a list of numbers, and n, an integer
Subject:Computer SciencePrice:15.89 Bought3
Please explain how I would write a recursive function num_pairs( L, n ) whose inputs are L, a list of numbers, and n, an integer. num_pairs should return the number of times two consecutive numbers in L have a sum of n.
Here are four examples of input and output for num_pairs:
In : num_pairs([], 5)
Out: 0
In : num_pairs([42], 42)
Out: 0
In : num_pairs([0,42,0], 42)
Out: 2
In : num_pairs([2,40,2,4,38], 42)
Out: 3
Below, implement this num_pairs function (no docstring needed) using recursion or a list of lists. (You may call front_sum(L,n) in your code, if you find that helpful!)
Please explain how I would use front_sum(L,n) in this code:
def front_sum(L, n):
if len(L) <2:
return False
if sum(L[:2]) == n:
return True
else:
return False
def num_pairs ( L, n ):
Purchased 3 times