Question

In: Computer Science

Define a Python function named matches that has two parameters. Both parameters will be lists of...

Define a Python function named matches that has two parameters. Both parameters will be lists of ints. Both lists will have the same length. Your function should use the accumulator pattern to return a newly created list. For each index, check if the lists' entries at that index are equivalent. If the entries are equivalent, append the literal True to your accumulator. Otherwise, append the literal False to your accumulator.
Hint: Since you must use the same index with each list, only write one loop in your function. The loop should use the built-in range function as its collection, since that will assign our loop variable to each index and, in the loop body, we can use the loop variable to get each input's entry at that index.

Solutions

Expert Solution

SOLUTION-
I have solve the problem in python code with comments and screenshot for easy understanding :)

CODE-

#matches function
def matches(list1, list2):
    #creating new list
    answer=[]
    #using the accumulator pattern to iterate with for loop to create the contents of new list
    for i in range(len(list1)):
        #comparing contents of both lists at same index and appending true of false accordingly
        if(list1[i]==list2[i]):
            answer.append(True)
        else:
            answer.append(False)
    #returning newly created list
    return answer
#calling matches function and printing the returned list
print("The newly created list is: ")
print(matches([4,5,6], [-3,5,2020]))

Code Screenshot:

Output:

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an...
Define a function named "find" that accepts two input parameters: "s "and "ch". "s" is an object of the type "string" and "ch" is a character value with the default argument of 'A'. The function looks for the character "ch" in the string  "s" and displays all the indices of its occurrences on the screen. For example, if the caller sends the values of "ABCDBGAB" and 'B' to the function for the parameters "s" and "ch", respectively, the function displays the...
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval...
Creates a function called biochild. The function has as parameters the number m and lists biomotℎer...
Creates a function called biochild. The function has as parameters the number m and lists biomotℎer and biofatℎer. The lists biomotℎer and biofatℎer contain 0’s and 1’s. For example: biomotℎer = [1,0,0,1,0,1] and biofatℎer = [1,1,1,0,0,1] Both lists have the same length n. The 0’s and 1’s represent bits of information (remember that a bit is 0 or 1). The function has to generate a new list (child). The child list has to have the same length n. child is...
please solve by utilizing python Define a function named checkOut which takes two JSON strings, each...
please solve by utilizing python Define a function named checkOut which takes two JSON strings, each representing a dictionary. The first has the following structure, { "customer" : name , "cart" : [ item1, item2, ... ] } The second has the following structure, { item1 : price1, item2 : price2, ... } The function must return a JSON string representing the following dictionary: { "customer" : name, "amount_due" : x } where x is the sum of the prices...
IN PYTHON Create a function called biochild.  The function has as parameters the number m...
IN PYTHON Create a function called biochild.  The function has as parameters the number m and the lists biomother and biofather.  The biomother and biofather lists contain 0’s and 1’s.  For example: biomother = [1,0,0,1,0,1] and biofather = [1,1,1,0,0,1]  Both lists have the same length n.  The 0's and 1's represent bits of information (remember that a bit is 0 or 1).  The function has to generate a new list (child).  The child...
How do I find matches between two different lists in Excel and highlight the matches using...
How do I find matches between two different lists in Excel and highlight the matches using vlookup?
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT