Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result.
Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15]
Output : [[7, 8, 9], [12, 18, 21]]
Explanation : [5, 3, 1] row has 3 in it in custom list, hence omitted.
Input : test_list = [[5, 3, 1], [7, 8, 19], [1, 10, 22], [12, 18, 20]], check_list = [3, 10, 19, 29, 20, 15]
Output : []
Explanation : All rows have some element from custom list, hence omitted.
In this, we perform the task of checking for any elements from custom list to check for rows using any() and list comprehension is used to omit row if any element from custom list is found in row.
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Similar to above method, only difference being filter() and lambda function is used to perform task of filtering out or omit rows from matrix from result.
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Here, the function omit_rows_recursive takes a matrix matrix and a list check_list as input. The base case is when the matrix is empty, in which case an empty list is returned. If the first row of the matrix contains any elements from the check list, the function recursively calls itself with the remaining part of the matrix after the first row. Otherwise, the function concatenates the first row with the result of recursively calling itself with the remaining part of the matrix after the first row.
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
The time complexity of the recursive function is also O(nm), as it must examine each element of each row in the matrix once.
The auxiliary space of the recursive function is also O(nm), as it may need to store up to n rows of m elements each in the result. However, the recursive function may have additional space complexity due to the function call stack, depending on the implementation.