In: Computer Science
Python:
def factors(matrix, factor):
The matrix is a 3D list of integers.
factors are either one, two, or three
If the factor is 'one' it will return a the first value from each list
if the factor is 'two' it will return the second value, and same for the third.
Example;
input = [ [ [1, 2, 3], [3, 2, 9], [9, 8, 6] ],
[ [0, 0, 4], [8, 9, 0], [5, 2, 1] ],
[ [0, 1, 1], [5, 5, 9], [3, 8, 4] ] ], 'one')
output = [ [1, 3, 9], [0, 8, 5], [0, 5, 3] ]
The answer to this question is as follows:
The code is as follows:
def function(matrix,factor):
l=[]
if(factor=='one'):
for i in matrix:
f=[]
for j in i:
f.append(j[0])
l.append(f)
elif(factor=='two'):
for i in matrix:
f=[]
for j in i:
f.append(j[1])
l.append(f)
elif(factor=='three'):
for i in matrix:
f=[]
for j in i:
f.append(j[2])
l.append(f)
return l
input = [ [ [1, 2, 3], [3, 2, 9], [9, 8, 6] ],
[ [0, 0, 4], [8, 9, 0], [5, 2, 1] ],
[ [0, 1, 1], [5, 5, 9], [3, 8, 4] ] ]
print("The output is",function(input,'two'))
The input and output is providd in the screenshot below:

