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 member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
Write a function sublist that takes two lists as arguments, and returns true if the first...
Write a function sublist that takes two lists as arguments, and returns true if the first list appears as a contiguous sublist somewhere within the second list, and false otherwise. > (sublist ’(c d e) ’(a b c d e f g)) #t > (sublist ’(a c e) ’(a b c d e f g)) #f Write a function lgrep that returns the “lines” within a list that contain a given sublist. Use the sublist function implemented in previous exercise...
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
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
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") # -> []...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT