Open In App

Python - Tuple elements inversions

Last Updated : 12 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among tuple elements. This is an essential utility as we come across bitwise operations many times. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using map() + list() + tuple() + lambda + "~" operator The above functions can be combined to perform this task. We can employ map() to accumulate the result of Inversion logic specified by the lambda function. The list() and tuple() functions are used to perform interconversions. 

Python3
# Python3 code to demonstrate working of 
# Tuple elements inversions
# Using map() + list() + tuple() + lambda + "~" operator 

# initializing tup 
test_tup = (7, 8, 9, 1, 10, 7) 

# printing original tuple
print("The original tuple is : " + str(test_tup)) 

# Tuple elements inversions
# Using map() + list() + tuple() + lambda + "~" operator 
res = tuple(list(map(lambda x: ~x, list(test_tup)))) 

# printing result 
print("The Bitwise Inversions of tuple elements are : " + str(res)) 
Output : 
The original tuple is : (7, 8, 9, 1, 10, 7)
The Bitwise Inversions of tuple elements are : (-8, -9, -10, -2, -11, -8)

  Method #2 : Using map() + tuple() + list() + operator.invert This task can also be performed using this method. In this the task performed by lambda function in above method is performed using ior function for cumulative Inversion operation. The list() and tuple() functions are used to perform interconversions. 

Python3
# Python 3 code to demonstrate working of 
# Tuple elements inversions
# Using map() + list() + tuple() + operator.invert 
from operator import invert 

# initializing tup 
test_tup = (7, 8, 9, 1, 10, 7) 

# printing original tuple
print("The original tuple is : " + str(test_tup)) 

# Tuple elements inversions
# Using map() + list() + tuple() + operator.invert 
res = tuple(list(map(invert, list(test_tup)))) 

# printing result 
print("The Bitwise Inversions of tuple elements are : " + str(res)) 
Output : 
The original tuple is : (7, 8, 9, 1, 10, 7)
The Bitwise Inversions of tuple elements are : (-8, -9, -10, -2, -11, -8)

METHOD 3:Using a for loop and a temporary list

APPROACH:

This approach involves iterating over each element in the input tuple using a for loop and appending its bitwise negation to a temporary list. Finally, the list is converted back to a tuple using the tuple() constructor and returned.

ALGORITHM:

1.Define a function that takes a tuple as input.
2.Create an empty list called 'inversions'.
3.Use a for loop to iterate through each element in the tuple and apply the bitwise negation operator (-x) to it. Append the result to the 'inversions' list.
4.Convert the 'inversions' list into a tuple and return it.

Python3
def tuple_inversions_3(tup):
    inversions = []
    for x in tup:
        inversions.append(-x)
    return tuple(inversions)

# Example Usage
tup = (7, 8, 9, 1, 10, 7)
print(tuple_inversions_3(tup))  # Output: (-7, -8, -9, -1, -10, -7)

Output
(-7, -8, -9, -1, -10, -7)

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


Next Article
Practice Tags :

Similar Reads