Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Modulo of tuple elements

Python – Modulo of tuple elements

Sometimes, while working with records, we can have a problem in which we may need to perform a modulo of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using zip() + generator expression 

The combination of the above functions can be used to perform this task. In this, we perform the task of modulo using generator expression and the mapping index of each tuple is done by zip(). 

Python3




# Python3 code to demonstrate working of
# Tuple modulo
# using zip() + generator expression
 
# Initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple modulo
# using zip() + generator expression
res = tuple(ele1 % ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
 
# Printing result
print("The modulus tuple : " + str(res))


Output : 

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)

Time complexity: O(n), where n is the length of the tuples.
Auxiliary space: O(n), as a new tuple is created to store the result.

Method #2: Using map() + mod

The combination of the above functionalities can also perform this task. In this, we perform the task of extending logic of modulus using mod and mapping is done by map(). 

Python3




# Python3 code to demonstrate working of
# Tuple modulo
# using map() + mod
from operator import mod
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple modulo
# using map() + mod
res = tuple(map(mod, test_tup1, test_tup2))
 
# printing result
print("The modulus tuple : " + str(res))


Output : 

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)

Time complexity: O(n), where n is the size of the tuples. This is because the program only performs a single pass through the tuples to compute the element-wise modulus using the map() and mod() functions.
Auxiliary space: O(n), where n is the size of the tuples. 

Method #3: Using for loop 

Python3




# Python3 code to demonstrate working of
# Tuple modulo
 
# Initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple modulo
res=[]
for i in range(0,len(test_tup1)):
    res.append(test_tup1[i]%test_tup2[i])
res=tuple(res)
 
# Printing result
print("The modulus tuple : " + str(res))


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)

Time complexity: O(n), where n is the length of the tuples. 
Auxiliary space: O(n)

Method #4: Using numpy

Note: Install numpy module using the command “pip install numpy”

It provides a convenient way to perform element-wise mathematical operations on arrays and lists.

Python3




import numpy as np
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple modulo using numpy
res = tuple(np.mod(test_tup1, test_tup2))
 
# printing result
print("The modulus tuple : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)

Time complexity: O(n) where n is the size of a tuple.
Auxiliary Space: O(n) as we are creating a new tuple of the same size as the original tuple.

Method #5: Using the Recursive method

Algorithm:

  1. Define a function called modulo_tuple that takes two tuples t1 and t2 as input, along with an optional result tuple which is initially empty.
  2. Check if the first tuple t1 is empty. If it is, return the result tuple.
  3. Otherwise, recursively call the modulo_tuple function with the first element of t1 and t2 removed, and the modulo of the first elements of t1 and t2 appended to result.
  4. Initialize two tuples test_tup1 and test_tup2 with some values.
  5. Print the original tuples test_tup1 and test_tup2.
  6. Call the modulo_tuple function with test_tup1 and test_tup2 as arguments and store the result in a variable called result.
  7. Print the result tuple.

Python3




# Python3 code to demonstrate working of
# Tuple modulo
# using recursive method
def modulo_tuple(t1, t2, result=()):
    if not t1:
        return result
    return modulo_tuple(t1[1:], t2[1:], result + (t1[0] % t2[0],))
 
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple modulo
result = modulo_tuple(test_tup1, test_tup2)
 
# printing result
print("The modulus tuple : " + str(result))
 
# this code contributed by tvsk


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #6: Using itertools.starmap()

Algorithm:

  1. Import itertools module.
  2. Define two tuples test_tup1 and test_tup2.
  3. Using starmap() function, map the modulus operation with each element of both the tuples using lambda function and zip() function.
  4. Convert the resultant object into a tuple and store it in the result variable.
  5. Print the result.

Python3




import itertools
 
# Initializing list
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# Printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
result = tuple(itertools.starmap(lambda x, y: x %
                                 y, zip(test_tup1, test_tup2)))
 
# Printing the result
print("The modulus tuple : " + str(result))
 
# This code is contributed by Jyothi pinjala.


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)

Time complexity: O(n)
Auxiliary Space: O(n)

Method #7: Using heapq

Algorithm:

  1. Initialize two tuples test_tup1 and test_tup2 with some elements.
  2. Calculate the modulus of the corresponding elements of the two tuples using zip() and a generator expression.
  3. Pass the generator expression to heapq.nlargest() with the len(test_tup1) argument to get the modulus of all the pairs of elements.
  4. Convert the result to a tuple using tuple() and assign it to modulus_tup.
  5. Print the original tuples and the modulus tuple.

Python3




import heapq
 
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
modulus_tup = tuple(heapq.nlargest(len(test_tup1), (x % y for x, y in zip(test_tup1, test_tup2))))
 
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
print("The modulus tuple : " + str(modulus_tup))
#This code is contributed by Rayudu.


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (5, 4, 1, 0)

Time Complexity: O(N)

The time complexity of this code is O(n log n), where n is the length of the input tuples. This is because heapq.nlargest() is used to calculate the modulus tuple, and its time complexity is O(n log k), where k is the length of the heap. In this case, k is equal to the length of the input tuples, so the time complexity is O(n log n).

Auxiliary Space: O(N)

The space complexity of this code is O(n), where n is the length of the input tuples. This is because a generator expression is used to calculate the modulus tuple, and its space complexity is O(1). The result of the generator expression is then converted to a tuple using tuple(), which has a space complexity of O(n). The space complexity of test_tup1, test_tup2, and modulus_tup is also O(n) since they all have n elements.

Method #8: Using List Comprehension

Approach:

  1. Initialize two tuples: test_tup1 and test_tup2 with integer values.
  2. Using list comprehension, apply modulus operator to each pair of elements from test_tup1 and test_tup2 tuples.
  3. Store the result in the res variable.
  4. Print the original tuples and the modulus tuple.

Python3




# Python3 code to demonstrate working of
# Tuple modulo
# using List comprehension
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple modulo
# using List comprehension
res = [ele1 % ele2 for ele1, ele2 in zip(test_tup1, test_tup2)]
res = tuple(res)
 
# printing result
print("The modulus tuple : " + str(res))


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The modulus tuple : (0, 4, 5, 1)

Time complexity: O(n), where n is the length of the tuples.
Auxiliary space: O(n), where n is the length of the tuples, because we are storing the result in a list before converting it to a tuple.

RELATED ARTICLES

Most Popular

Recent Comments