Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / 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

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

Computer Science

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 ):

Option 1

Low Cost Option
Download this past answer in few clicks

17.89 USD

PURCHASE SOLUTION

Already member?


Option 2

Custom new solution created by our subject matter experts

GET A QUOTE