45.9k views
0 votes
g Write a Racket function (process lst1 lst2) that processes the corresponding elements of the two lists and returns a list. If the corresponding elements are both numbers, include the sum of the two corresponding elements in the list that is returned.

User Shivanka
by
5.6k points

1 Answer

4 votes

Answer:

  1. def process(lst1, lst2):
  2. newList = []
  3. for i in range(0, len(lst1)):
  4. sum = lst1[i] + lst2[i]
  5. newList.append(sum)
  6. return newList
  7. list1 = [1, 3, 5, 7, 9]
  8. list2 = [2, 4, 6, 8, 10]
  9. print(process(list1, list2))

Step-by-step explanation:

Firstly, create a function process that takes two input lists (Line 1). In the function, create a new list (Line 2). Use a for loop to traverse through the elements of the two lists and total up the corresponding elements and add the sum to the new list (Line 3 - 5). At last, return the new list (Line 6).

In the main program, create two sample list and use them as arguments to test the function process (Line 8 - 9). We shall get the output [3, 7, 11, 15, 19].

User Priyabagus
by
5.5k points