Question

In: Computer Science

Python: def factors(matrix, factor): The matrix is a 3D list of integers. factors are either one,...

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] ]

Solutions

Expert Solution

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:


Related Solutions

Python program please no def, main, functions Given a list of negative integers, write a Python...
Python program please no def, main, functions Given a list of negative integers, write a Python program to display each integer in the list that is evenly divisible by either 5 or 7. Also, print how many of those integers were found. Sample input/output: Enter a negative integer (0 or positive to end): 5 Number of integers evenly divisible by either 5 or 7: 0 Sample input/output: Enter a negative integer (0 or positive to end): -5 -5 is evenly...
def largest_rectangle_in_matrix(matrix: List[List[int]]) -> int: """ Returns the area of the largest rectangle in <matrix>. The...
def largest_rectangle_in_matrix(matrix: List[List[int]]) -> int: """ Returns the area of the largest rectangle in <matrix>. The area of a rectangle is defined as the number of 1's that it contains. Again, you MUST make use of <largest_rectangle_at_position> here. If you managed to code largest_rectangle_at_position correctly, this function should be very easy to implement. Similarly, do not modify the input matrix. Precondition: <matrix> will only contain the integers 1 and 0. >>> case1 = [[1, 0, 1, 0, 0], ... [1,...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
#Write a program in Python that given a list of positive integers, return another list where...
#Write a program in Python that given a list of positive integers, return another list where each element corresponds to the sum of the digits of the elements o#f the given list. #Example: # input # alist=[124, 5, 914, 21, 3421] # output # sum=[7, 5, 14, 3, 18]
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Python: How would I write a function using list comprehensions to list of integers into two...
Python: How would I write a function using list comprehensions to list of integers into two halves with the evens first, followed by odds? And on top of that, the numbers should be listed in their original order.
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value...
In Python write a function with prototype “def dictsort(d):” which will return a list of key-value pairs of the dictionary as tuples (key, value), reverse sorted by value (highest first) and where multiple keys with the same value appear alphabetically (lowest first).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT