Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power 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 above functions can be used to perform this task. In this, we perform the task of power using generator expression and mapping index of each tuple is done by zip().
Step by step approach :
- Initialize two tuples test_tup1 and test_tup2 with values (10, 4, 5, 6) and (5, 6, 7, 5) respectively.
- Print the original tuples using the print() function and concatenation.
- Create a generator expression using the zip() function to iterate over the tuples test_tup1 and test_tup2 in parallel. This pairs up the corresponding elements of each tuple to form a new tuple for each index.
For each tuple of paired elements, raise the first element to the power of the second element using the exponentiation operator **.
- Convert the resulting generator expression into a new tuple res using the tuple() function.
- Print the resulting exponentiated tuple res using the print() function and concatenation.
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 exponentiated tuple : " + str (res))
|
Output :
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : (100000, 4096, 78125, 7776)
Time complexity: O(n), where n is the length of the input tuples.
Auxiliary space: O(n), where n is the length of the input tuples.
Method #2: Using map() + pow The combination of above functionalities can also perform this task. In this, we perform the task of extending the logic of exponentiation using mul and mapping is done by map().
Python3
from operator import pow
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 ( pow , test_tup1, test_tup2))
print ("The exponentiated tuple : " + str (res))
|
Output :
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : (100000, 4096, 78125, 7776)
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3 : Using numpy
Note: install numpy module using command “pip install numpy”
The numpy library in python can be used to perform exponentiation on tuples.
Python3
import numpy as np
test_tup1 = ( 10 , 4 , 5 , 6 )
test_tup2 = ( 5 , 6 , 7 , 5 )
res = tuple (np.power(test_tup1, test_tup2))
print ( "The exponentiated tuple : " + str (res))
|
Output:
The exponentiated tuple : (100000, 4096, 78125, 7776)
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using operator.pow() method
Approach
- Initiated a for loop
- Raised first tuple elements to the power of second tuple elements and appended to output list
- Displayed the output list
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)):
import operator
res.append(operator. pow (test_tup1[i],test_tup2[i]))
print ( "The exponentiated tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : [100000, 4096, 78125, 7776]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using list comprehension:
Iterate over the indices of the tuples using range(len(test_tup1)) and calculate the exponential of corresponding elements using the ** operator. The result is stored in a list and then converted to a tuple using the tuple() function.
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 ([test_tup1[i] * * test_tup2[i] for i in range ( len (test_tup1))])
print ( "The exponentiated tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : (100000, 4096, 78125, 7776)
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 6: Using list comprehension is using the built-in map() function along with lambda function:
The map() function applies the lambda function to each corresponding element of the tuples test_tup1 and test_tup2. The resulting values are then used to create a new tuple res.
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 ( map ( lambda x, y: x * * y, test_tup1, test_tup2))
print ( "The exponentiated tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : (100000, 4096, 78125, 7776)
Time complexity: O(n), where n is the length of the tuples test_tup1 and test_tup2. This is because both approaches iterate over the tuples once.
Auxiliary space: O(n) because it creates a new list using list comprehension before converting it to a tuple.
Method 7: Using Recursive method.
Algorithm for the recursive tuple_exponentiation function:
- If the length of tup1 is 0, return an empty list.
- Otherwise, calculate the exponentiation of the first elements of tup1 and tup2 using the pow() function from the operator module, and add it to the result of calling tuple_exponentiation with the remaining elements of tup1 and tup2.
- Return the resulting list.
Python3
def tuple_exponentiation(tup1, tup2):
if len (tup1) = = 0 :
return []
else :
import operator
return [operator. pow (tup1[ 0 ], tup2[ 0 ])] + tuple_exponentiation(tup1[ 1 :], tup2[ 1 :])
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_exponentiation(test_tup1, test_tup2)
print ( "The exponentiated tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : [100000, 4096, 78125, 7776]
The time complexity of this algorithm is O(n), where n is the length of the tuples, since the function needs to process each element in both tuples exactly once.
The space complexity is also O(n), due to the use of recursion and the creation of the result list. However, the constant factors involved in the space complexity will be larger than for the iterative solution, since each recursive call requires additional memory for the function call stack.
Method 8: using a for loop
Using a for loop and append the result of each element in the new list.
Python3
def tuple_exponentiation(tup1, tup2):
result = []
for i in range ( len (tup1)):
result.append(tup1[i] * * tup2[i])
return result
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_exponentiation(test_tup1, test_tup2)
print ( "The exponentiated tuple : " + str (res))
|
Output
The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : [100000, 4096, 78125, 7776]
Time complexity: O(n) where n is the length of the tuples.
Auxiliary space: O(n) where n is the length of the tuples, as we create a new list to store the result.
Method 9: Using itertools:
1.Import the starmap function from the itertools module.
2.Define two tuples test_tup1 and test_tup2 with the numbers to be exponentiated.
3.Print the original tuples.
4.Use the zip function to create an iterable of tuples with the corresponding elements of test_tup1 and test_tup2. Then, use starmap and pow to apply the exponentiation operation to each pair of elements in the iterable. Finally, create a tuple with the results.
5.Print the resulting tuple.
Python3
from itertools import starmap
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 (starmap( pow , zip (test_tup1, test_tup2)))
print ( "Exponentiation using itertools and starmap: " + str (res))
|
Output
The original tuple 1: (10, 4, 5, 6)
The original tuple 2: (5, 6, 7, 5)
Exponentiation using itertools and starmap: (100000, 4096, 78125, 7776)
Time complexity: O(n), where n is the length of the tuples
Space complexity: O(n), as the result is stored in a new tuple of length n
Please Login to comment...