Given a String, extract tuple list, with each tuple being of custom length, delimited using comma.
Input : test_str = "6 6 7, 3 4, 2"
Output : [(6, 6, 7), (3, 4), (2, )]
Explanation : The customs lengths being 3, 2, 1 have been converted to tuple list.
Input : test_str = "7, 7, 4"
Output : [(7, ), (7, ), (4, )]
Explanation : All elements are of length 1.
The combination of above functions can be used to solve this problem. In this, we perform conversion of string characters using int() and tuple() and split() is used to split on delimiter.
OutputThe original string is : 4 6 7, 1 2, 3, 4 6 8 8
The constructed custom length tuples : [(4, 6, 7), (1, 2), (3, ), (4, 6, 8, 8)]
The combination of above functions provide yet another way in which this task can be performed. In this we perform task using similar method as above just difference being using map() to extend logic of integer conversion to elements.
OutputThe original string is : 4 6 7, 1 2, 3, 4 6 8 8
The constructed custom length tuples : [(4, 6, 7), (1, 2), (3, ), (4, 6, 8, 8)]
Time Complexity: O(n*n) where n is the number of elements in the list “test_str”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_str”.
OutputThe original string is : 4 6 7, 1 2, 3, 4 6 8 8
The constructed custom length tuples : [(4, 6, 7), (1, 2), (3,), (4, 6, 8, 8)]
Time complexity: O(nm), where n is the number of substrings and m is the maximum length of each substring
Auxiliary space: O(nm), as we are creating a new list to store the resulting tuples