Question

In: Computer Science

Below is the code i got from another expert Q&A but im not allowed to use...

Below is the code i got from another expert Q&A but im not allowed to use 'del' function because i didnt learn it.

+ I need to use recursion not list comprehenshion.

Please carefully go over the instructions

def add(vals1,vals2):
  
  
    if (len(vals1))==0:
        return []
    else:
        x = vals1[0]+vals2[0]
        del vals1[0],vals2[0]
      
      

        return [x]+add(vals1,vals2)

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

Solutions

Expert Solution

Here is the code to do so.

The codes are well commented and easy to understand, if the answer helped you please upvote and if you have any doubts please comment i will surely help. please take care of the indentation while copying the code. Check from the screenshots provided.

Code:

def addLists(list1, list2):
# When the list size is 0, i.e. the end of recursion is achieved
# return an empty list
if len(list1)==0:
return []
else:
# if the end of the list is not reached, concatenate the sum of the
# first element of both lists to result, by performing a recursive
# call on the lists starting from the next position
return [list1[0] + list2[0]] + addLists(list1[1:],list2[1:] )
  
print(addLists([1, 3, 4, 2, 5],[1,2,3,4,5]))


Related Solutions

please , im sorting by name, address, dependent and hours. i got a lot of errors...
please , im sorting by name, address, dependent and hours. i got a lot of errors i need help please. Thanks package application; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; class Emprec { String name; String address; double hours; double rate; int dependents; char gender; boolean degree; // This is the classes's constructor !!!! Emprec(String name, String address, String hours,String dependents) { try { this.name = name; this.address = address; this.hours = Double.valueOf(hours).doubleValue(); this.dependents = Integer.parseInt(dependents); } catch (NumberFormatException...
I got the read-ahead package from the project staff. And I have a few questions. Q#1...
I got the read-ahead package from the project staff. And I have a few questions. Q#1 (100 points)   This table shows some of the info for the week. 20 sample M923’s were produced. It looks like the parts are in some kind of multiple per unit. The first section shows the quantity of each part and the total cost. But doesn’t anybody think that I’d be interested in knowing the price for the part? And I see the direct labor...
I need this code translated from C++ to Java. Im personally still trying to learn Java,...
I need this code translated from C++ to Java. Im personally still trying to learn Java, so if you can include screenshots of your IDE/output that would be helpful. Much appreciated! #include <iostream> #include <string> using namespace std; class pizza { public:    string ingrediants, address;    pizza *next;    pizza(string ingrediants, string address)    {        this->address = address;        this->ingrediants = ingrediants;        next = NULL;    } }; void enqueue(pizza **head, pizza **tail, pizza...
How do i get the code below to add an element before another without making the...
How do i get the code below to add an element before another without making the array off one element? (javascript) public void addBefore(double element) { itemCount++; double data[] = new double[this.data.length]; if(currentIndex <= itemCount) { if(currentIndex != 0) { for(int index = currentIndex; index >= itemCount; index ++) { data[index] = this.data[index]; } currentIndex--; data[currentIndex] = element; } if(currentIndex == 0) { data[0] = element; currentIndex = 0; } } }
Assume an economy where GDP (Q) is measured by Q= C+I+G+ (X - Im) .Assume Consumption...
Assume an economy where GDP (Q) is measured by Q= C+I+G+ (X - Im) .Assume Consumption (C) is defined by C = 800 +0.95Qd and Investment (I) is defined as I = 1500. Government Purchases (G) is given as G = 2000, Transfer payments (Tr) is given as Tr = 3300, Taxes (Tx) are given by Tx = 300 + 0.4Q Exports (X) are defined as X = 750 and Imports (Im) are defined as Im = 400 + 0.07Q...
I have a program to code for my computer science class, and Im not sure how...
I have a program to code for my computer science class, and Im not sure how to do it. If someone can explain it step by step I would appreciate it. Problem Description: Define the GeometricObject2D class that contains the properties color and filled and their appropriate getter and setter methods. This class also contains the dateCreated property and the getDateCreated() and toString() methods. The toString() method returns a string representation of the object. Define the Rectangle2D class that extends...
PLEASE NOTE: I POSTED THIS QUESTION BEFORE BUT ONE EXPERT SIMPLY COPIED THE ANSWER TO ANOTHER...
PLEASE NOTE: I POSTED THIS QUESTION BEFORE BUT ONE EXPERT SIMPLY COPIED THE ANSWER TO ANOTHER QUESTION THAT WAS USING THE SAME INFORMATION. WHAT I'M LOOKING FOR ARE 2 GRAPHS. An investor is provided with the following information on American put and call options on a share of a company listed on the London Stock Exchange: Call price (c0) = 33p Put price (p0) = 49p Exercise price (X) = 480p Today: 11 June 2019 Expiry date: 20 December 2019...
C++ Please complete based on the code below. Declare another stack object to the code in...
C++ Please complete based on the code below. Declare another stack object to the code in main(). Add a stack operator called CopyStack to the Stack class which, when executed, copies the contents of the first stack into the second stack. Modify your menu so that this option is available. The menu should also allow the second stack to be printed, pushed, popped, and so forth, just like with the first stack. #include using namespace std; #define MAXSize 10 class...
We are to make a program about a car dealership using arrays. I got the code...
We are to make a program about a car dealership using arrays. I got the code to display all cars in a list, so I'm good with that. What I'm stuck at is how to make it so when a user inputs x for search, it allows them to search the vehicle. We need two classes, one that shows the car information and another that shows the insert, search, delete, display methods. Here is what I have so far package...
Hi, I would like the following python code rewritten in a different way/structure etc. I got...
Hi, I would like the following python code rewritten in a different way/structure etc. I got it from a question I posted but am worried fellow classmates will also be using it so am covering bases. her it is. #threeUniqueSongs.py #Simulation of professor listening to 3 songs out of playlist of 4 songs 10000 times sampling with replacement possible import random import math #Here playlist is the list of 4 songs i.e. "Eastside", "Better Now", "Lucid Dreams", "Harder, Better, Faster,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT