Thursday, October 23, 2025
HomeLanguagesPython program to construct Equidigit tuples

Python program to construct Equidigit tuples

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().

Python3




# Python3 code to demonstrate working of
# Construct Equidigit tuples
# Using loop + slicing str()
 
# initializing list
test_list = [5654, 223, 982143, 34, 1021]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
for sub in test_list:
     
    # getting mid element
    mid_idx = len(str(sub)) // 2
     
    # slicing Equidigits
    el1 = str(sub)[:mid_idx]
    el2 = str(sub)[mid_idx:]
     
    res.append((int(el1), int(el2)))
 
# printing result
print("Equidigit tuples List : " + str(res))


 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:

  • Initialize the list of integers test_list with the given values.
  • Print the original list using the print() function and string concatenation.
  • Use a list comprehension to iterate over the list of integers test_list.
  • In each iteration, compute the length of the integer using the len() function and integer division // by 2. This gives us the index of the middle digit.
  • Use the divmod() function to split the integer into its two halves. The first argument to divmod() is the integer, and the second argument is the power of 10 corresponding to the index of the middle digit.
  • The list comprehension returns a list of tuples containing the two halves of each integer.
  • Print the resulting list of tuples using the print() function and string concatenation.

Python3




# Python3 code to demonstrate working of
# Construct Equidigit tuples
# Using list comprehension + divmod() function
 
# initializing list
test_list = [5654, 223, 982143, 34, 1021]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using list comprehension + divmod() function
res = [divmod(sub, 10 ** (len(str(sub)) // 2)) for sub in test_list]
 
# printing result
print("Equidigit tuples List : " + str(res))


Output

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.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS