Open In App

Python | Conversion to N*N tuple matrix

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data, we can have a problem in which we have data in form of tuple Matrix with uneven length rows. In this case there’s a requirement to complete the N*N matrix with a default value. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using loop + * operator This problem can be solved using loop. This is brute force method to perform this task. We just append the default value as many times, as the data is missing in a row than N. 

Python3




# Python3 code to demonstrate working of
# Conversion to N * N tuple matrix
# using loop + * operator
 
# initialize tuple
test_tup = ((5, 4), (3, ), (1, 5, 6, 7), (2, 4, 5))
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# initializing dimension
N = 4
 
# Conversion to N * N tuple matrix
# using loop + * operator
res = []
for tup in test_tup :
    res.append( tup +(0, ) * (N - len(tup)))
 
# printing result
print("Tuple after filling values : " + str(res))


Output

The original tuple is : ((5, 4), (3,), (1, 5, 6, 7), (2, 4, 5))
Tuple after filling values : [(5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0)]

  Method #2 : Using tuple() + generator expression Similar task can be performed in one line using generator expression. In this, similar logic is applied as above just zipped as one-liner. The tuple(), changes result to tuple. 

Python3




# Python3 code to demonstrate working of
# Conversion to N * N tuple matrix
# using tuple() + generator expression
 
# initialize tuple
test_tup = ((5, 4), (3, ), (1, 5, 6, 7), (2, 4, 5))
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# initializing dimension
N = 4
 
# Conversion to N * N tuple matrix
# using tuple() + generator expression
res = tuple(sub + (0, ) * (N-len(sub)) for sub in test_tup)
 
# printing result
print("Tuple after filling values : " + str(res))


Output

The original tuple is : ((5, 4), (3,), (1, 5, 6, 7), (2, 4, 5))
Tuple after filling values : ((5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0))

Method #3 : Using map()+lambda

Approach

Using map() function

Algorithm

1. Define the original tuple and the value of N.
2. Use the map() function to apply a lambda function to each tuple in the original tuple.
3. The lambda function checks the length of the tuple and adds zeros if the length is less than N.
4. The resulting tuple is stored in the result variable.

Python3




# Python3 code to demonstrate working of
# Conversion to N * N tuple matrix
# using map + lambda
 
# initialize tuple
test_tup = ((5, 4), (3, ), (1, 5, 6, 7), (2, 4, 5))
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# initializing dimension
N = 4
 
# Conversion to N*N Matrix using map + lambda
res = tuple(map(lambda t: t + (0,) * (N - len(t)), test_tup))
 
# printing result
print("Tuple after filling values : " + str(res))


Output

The original tuple is : ((5, 4), (3,), (1, 5, 6, 7), (2, 4, 5))
Tuple after filling values : ((5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0))

Time complexity: O(N*M), where N is the length of the original tuple and M is the maximum length of any tuple in the original tuple. This is because we are iterating over each tuple in the original tuple and adding zeros if necessary.

Space complexity: O(N*M), where N is the length of the original tuple and M is the maximum length of any tuple in the original tuple. This is because we are creating a new tuple with zeros appended to each tuple in the original tuple, and this new tuple has N tuples with M elements each.

METHOD 4: Using def function

APPROACH:

This approach first finds the maximum length of any tuple in the input tuple. It then loops through each tuple in the input tuple and checks if the length of the tuple is less than the maximum length. If it is, it appends the tuple with the necessary number of zeroes to make its length equal to the maximum length. Otherwise, it just appends the tuple as is. Finally, it returns the resulting matrix as a tuple.

ALGORITHM:

1.Find the maximum length of any tuple in the input tuple.
2.Initialize an empty list result.
3.Loop through each tuple x in the input tuple:
a. If the length of x is less than max_len, add (max_len – len(x)) zeros to x and append the resulting tuple to result.
b. Otherwise, simply append x to result.
4.Convert the result list to a tuple and return it.

Python3




def convert_to_matrix(tup):
    max_len = max([len(x) for x in tup])
    result = []
    for x in tup:
        if len(x) < max_len:
            result.append(x + (0,) * (max_len - len(x)))
        else:
            result.append(x)
    return tuple(result)
 
# Example usage
original_tuple = ((5, 4), (3,), (1, 5, 6, 7), (2, 4, 5))
result = convert_to_matrix(original_tuple)
print(result)


Output

((5, 4, 0, 0), (3, 0, 0, 0), (1, 5, 6, 7), (2, 4, 5, 0))

The time complexity of this algorithm is O(nm), where n is the length of the input tuple and m is the maximum length of any tuple in the input tuple. The auxiliary space is also O(nm), as a new matrix with the same size as the input tuple is created.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads