![]() |
VOOZH | about |
Given the Elements list, divide the tuple list into similar digit tuples pairs.
Input : test_list = [5654, 223, 982143, 34, 1021]
Output : [(56, 54), (2, 23), (982, 143), (3, 4), (10, 21)]
Explanation : Element in tuples equidistributed.Input : test_list = [5654, 223, 1021]
Output : [(56, 54), (2, 23), (10, 21)]
Explanation : Element in tuples equidistributed.
Method #1 : Using loop + slicing + str()
In this, we perform the task of dividing by getting mid-idx and then slice from mid, initially integral number is split to a string by using str().
Output:
The original list is : [5654, 223, 982143, 34, 1021] Equidigit tuples List : [(56, 54), (2, 23), (982, 143), (3, 4), (10, 21)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension + divmod() function
Ue a list comprehension to iterate over the list of integers and construct the equidigit tuples using the divmod() function. The divmod() function takes two arguments and returns a tuple containing the quotient and remainder of the division. We can use this function to split an integer into its two halves.
Step-by-step approach:
The original list is : [5654, 223, 982143, 34, 1021] Equidigit tuples List : [(56, 54), (22, 3), (982, 143), (3, 4), (10, 21)]
Time complexity: O(n*log(m)), where n is the length of the input list and m is the maximum number of digits in any integer in the list.
Auxiliary space: O(n), since we are creating a new list of tuples containing the equidigit halves.