Question

In: Computer Science

This is the question Write a function add(vals1, vals2) that takes as inputs two lists of...

This is the question

Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length.

For example:

>>> add([1, 2, 3], [3, 5, 8])
result: [4, 7, 11]

Note that:

  • The first element of the result is the sum of the first elements of the original lists (1 + 3 –> 4).
  • The second element of the result is the sum of the second elements of the original lists (2 + 5 –> 7).
  • The third element of the result is the sum of the third elements of the original lists (3 + 8 –> 11)

And this is why i have for the code. But i have not learned del function and im afraid i cant use it.

im only allowed to use

  • arithmetic operators: +, -, *, **, /, //, %
  • boolean operators: <, >, <=, >=, ==, !=
  • comments and docstrings
  • conditional statements: if, elif, else
  • len()
  • logical operators: and, or, not
  • list operations: indexing, slicing, skip-slicing, concatenation, construction
  • print()
  • recursion
  • string operations: indexing, slicing, concatenation, duplication
  • type()

def add(vals1,vals2):
    """return a new list in which each elements is the sum of
        the corresponding elements of val1 and vals2
        input: lists of 0 or more numbers
    """
    if (len(vals1))==0:
        return []
    else:
        x = vals1[0]+vals2[0]
        del vals1[0],vals2[0]
      
      

        return [x]+add(vals1,vals2)

Solutions

Expert Solution

The function is as follows:

# Definition of the function.

def add(vals1, vals2):

    # check the condition

    if(len(vals1)==0):

        # return empty array.

        return []

    else:

        # calculate the value.

        x = vals1[0]+ vals2[0];

   # return the value recursively.

    return [x] + add(vals1[1:], vals2[1:])

The complete executable code is as follows:

Program screenshots:

Sample Output:

Code to Copy:

# kindly use python 3.6

# kindly indent the code as given in above screenshot.

# Definition of the function.

def add(vals1, vals2):

    # check the condition

    if(len(vals1)==0):

        # return empty array.

        return []

    else:

        # calculate the value.

        x = vals1[0]+ vals2[0];

   # return the value recursively.

    return [x] + add(vals1[1:], vals2[1:])

# call the function.

print(add([1, 2, 3], [3, 5, 8]))


Related Solutions

Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers,...
Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the result is the sum...
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Write a C++ function template to add two inputs and return their result. Make exceptions for...
Write a C++ function template to add two inputs and return their result. Make exceptions for Characters (such that sum of two characters is a character associated with sum of their ASCII) and String (such that sum of two strings is their concatenation)
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
Write a function myfn6 which takes as inputs vector u and value a, and output as...
Write a function myfn6 which takes as inputs vector u and value a, and output as vector w with its elements being “True, ” or “False, ”(w = [True, False, False, …, True]). Such that “True, ” means a is in u and “False, ” means a is not in u. Test your code for u = [0, -3, 1, 1, 2, 2, 6, 2] and a = 9, a = 1 and a = 2. Copy your code together...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
Write a function in Python that adds two Binary Trees together. The inputs of your function...
Write a function in Python that adds two Binary Trees together. The inputs of your function should be two binary trees (or their roots, depending on how you plan on coding this), and the output will be one Binary Tree that is the sum of the two inputted. To add Binary Trees together: Add the values of nodes with the same position in both trees together, this will be the value assigned to the node of the same position in...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi and alofi, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. sort function is not allowed as well as list comprehensions math is the only library that can be imported Example: int([4, 13,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT