In: Computer Science
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.
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!!!!!!!!----------