![]() |
VOOZH | about |
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column.
Example:
Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]]
Output : [(4, 7, 10, 18), (5, 8, 13, 17)]
Explanation : All column number elements contained together.Input : test_list = [[(4, 5)], [(10, 13)]]
Output : [(4, 10), (5, 13)]
Explanation : All column number elements contained together.
In this, we perform task of flattening using list comprehension and zip() is used to perform column pairing to render as tuple pairs.
Steps:
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]] The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]
In this, task of flattening is performed using chain.from_iterable() and zip() is used to perform the task of column pairing.
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]] The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]
Time complexity: O(nm)
Auxiliary space: O(nm),
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]] The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]
The time complexity is O(n^2), where n is the total number of elements in the input matrix.
The auxiliary space complexity of this code is O(n), as it uses a list x to store all the elements in the matrix and two lists re1 and re2 to store the elements after converting them to a tuple list.
This code converts a matrix represented as a tuple of tuples, matrix, into a tuple of lists, list_tuple. It uses map() and a lambda function to apply list() to each tuple in matrix, converting them into lists. The resulting tuple of lists is then printed.
([1, 2, 3], [4, 5, 6], [7, 8, 9])
Time complexity: O(mn), where m is the number of rows in the matrix and n is the number of columns in the matrix. This is because the map function is applied to each inner tuple in the matrix, and converting each inner tuple to a list takes O(n) time. Since there are m inner tuples, the total time complexity is O(mn).
Auxiliary space: O(mn), which represents the memory used to store the final tuple of lists. This is because each inner tuple is converted to a list, and all the lists are stored in the final tuple. The amount of memory used is proportional to the size of the matrix.
Step-by-step algorithm:
output:
The converted tuple list : [([4, 10, 0], [7, 18, 10]), ([5, 13, 4], [8, 17, 1])
Time complexity: O(n), where n is the total number of elements in the input list test_list. This is because the algorithm involves iterating over each element in the list once to flatten it into a 1-dimensional numpy array, then transposing the array, and finally converting the transposed array into a list of tuples. The time complexity of these operations is linear with respect to the number of elements in the input list.
Auxiliary space: O(n), where n is the total number of elements in the input list test_list. This is because the algorithm creates a numpy array of the same size as the flattened input list, which requires additional memory. The space used by the result variable is also proportional to the number of elements in the input list. Overall, the space complexity of the algorithm is linear with respect to the number of elements in the input list.
Step-by-step algorithm:
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]] The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]
Time complexity: O(n), where n is the total number of elements in the input list.
Auxiliary space: O(n), since the flatten () function creates a new list containing all the elements of the input list.