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
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple (ele1 % ele2 for ele1, ele2 in zip (test_tup1, test_tup2))
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
from operator import mod
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( map (mod, test_tup1, test_tup2))
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
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = []
for i in range ( 0 , len (test_tup1)):
res.append(test_tup1[i] % test_tup2[i])
res = tuple (res)
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
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple (np.mod(test_tup1, test_tup2))
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 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:
- 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.
- Check if the first tuple t1 is empty. If it is, return the result tuple.
- 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.
- Initialize two tuples test_tup1 and test_tup2 with some values.
- Print the original tuples test_tup1 and test_tup2.
- Call the modulo_tuple function with test_tup1 and test_tup2 as arguments and store the result in a variable called result.
- Print the result tuple.
Python3
def modulo_tuple(t1, t2, result = ()):
if not t1:
return result
return modulo_tuple(t1[ 1 :], t2[ 1 :], result + (t1[ 0 ] % t2[ 0 ],))
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
result = modulo_tuple(test_tup1, test_tup2)
print ( "The modulus tuple : " + str (result))
|
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:
- Import itertools module.
- Define two tuples test_tup1 and test_tup2.
- Using starmap() function, map the modulus operation with each element of both the tuples using lambda function and zip() function.
- Convert the resultant object into a tuple and store it in the result variable.
- Print the result.
Python3
import itertools
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
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)))
print ( "The modulus tuple : " + str (result))
|
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:
- Initialize two tuples test_tup1 and test_tup2 with some elements.
- Calculate the modulus of the corresponding elements of the two tuples using zip() and a generator expression.
- Pass the generator expression to heapq.nlargest() with the len(test_tup1) argument to get the modulus of all the pairs of elements.
- Convert the result to a tuple using tuple() and assign it to modulus_tup.
- 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))
|
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:
- Initialize two tuples: test_tup1 and test_tup2 with integer values.
- Using list comprehension, apply modulus operator to each pair of elements from test_tup1 and test_tup2 tuples.
- Store the result in the res variable.
- Print the original tuples and the modulus tuple.
Python3
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = [ele1 % ele2 for ele1, ele2 in zip (test_tup1, test_tup2)]
res = tuple (res)
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.
Please Login to comment...