In: Computer Science
Variable trials refers to a list where each list item is another list of 3 integers between 0 and 9. Imagine each list of 3 integers is a combination of numbers of a lock. Further suppose the variable code refers to a list of three integers that is the correct combination of the lock.
Write a program which, assuming trials and code are already defined, prints out a character for each digit of each trial. Print Y if the digit in the trail matches the digit at the same position in the code. Print # if the digit in the trial does not match the digit at the same position in the code but appears elsewhere in the code. Print N if the digit of the trial does not match the digit in the code and the digit in the trial appears nowhere in the code. Print a newline after the characters after three numbers (for each trial).
Example, if trails = [[2,3,4], [5,5,5], [5,2,3]], and code = [2,3,5], the program output should be:
Y Y N
# # Y
# # #
Hint: the in and not in operators can be used to test whether an item appears in a list.
(language python.)
Below is a screen shot of the python program to check indentation. Comments are given on every line explaining the code.Below is the output of the program:
Below is the code to copy: #CODE STARTS HERE----------------
#Predefined trails and code
trails = [[2,3,4],[5,5,5],[5,2,3]]
code = [2,3,5]
#Loop through the trails
for trail in trails:
#Loop through each digit in the trail using index
for x in range(len(trail)):
# Checks if elements at the same position are matching
if trail[x] == code[x]:
print("Y",end=" ")
#Checks if digit is anywhere in 'code'
elif trail[x] in code:
print("#",end=" ")
#Checks if digit is not in 'code'
elif trail[x] not in code:
print("N",end=" ")
print() #Prints new line after each trail
#CODE ENDS HERE------------------