Python Program to find XOR of values of all even keys in a dictionary
Last Updated :
26 Apr, 2023
Given a dictionary in Python, our task is to find the XOR of values of all even keys in a dictionary in Python.
Note: All the keys and values in the dictionary are integers.
Examples:
Input : dic= {1:3, 4:5, 6:7, 3 :8}
Output : 2
Explanation: Even keys in the dictionary are 4,6 and their values are 5,7 Thus,
XOR of values will be 5^7 = 2
Method 1: Naive Approach
- Start traversing the dictionary.
- Maintain a result variable to store the answer.
- Check if the current key is even, if it is even then XOR that value with the result.
- Finally, return the result.
Below is the implementation of the above approach as follows:
Python3
def isEven(x):
if x % 2 = = 0 :
return True
return False
def xorOfDictionaryKeys(dic):
result = 0
for i in dic:
if isEven(i):
result = result ^ dic[i]
return result
dic = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 }
print (xorOfDictionaryKeys(dic))
|
Time complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using items() function
Python3
def isEven(x):
if x % 2 = = 0 :
return True
return False
def xorOfDictionaryKeys(dic):
result = 0
for key, value in dic.items():
if isEven(key):
result = result ^ value
return result
dic = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 }
print (xorOfDictionaryKeys(dic))
|
Time complexity: O(n)
Auxiliary Space: O(1)
Method 3: Using the filter() function
Step-by-Step Approach :
- First, let’s define a dictionary named my_dict.
- Now, use the filter() function to create an iterator even_values containing only the elements in my_dict that are even in nature.
- Initialize a variable Gfgresult to 0.
- Use a for loop to iterate over even_values.
- For each key, value in even_values, compute its XOR with the Gfgresult using the bitwise XOR operator ^ and store the result back in Gfgresult.
- Print the value of Gfgresult.
Python3
my_dict = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 }
even_values = filter ( lambda kv: kv[ 0 ] % 2 = = 0 , my_dict.items())
Gfgresult = 0
for key, value in even_values:
Gfgresult ^ = value
print (Gfgresult)
|
Complexity Analysis:
Time Complexity: O(n)
This is because the filter function takes O(n) time to create the iterator containing the elements that are even, where n is the length of the input array. Now the for loop iterates over the numbers that are even for a constant number of iteration, so we can say that the overall time complexity will be O(n).
Auxiliary Space: O(1)
This is because we are only using a few variables to store the input dictionary, even_values, and the Gfgresult and the space used by these variables does not depend on the size of the input dictionary. So we can say that the overall space complexity will be O(1).
Method 4: Using dictionary comprehension
- Initialize a dictionary.
- Create a new dictionary even_dict that includes only the key-value pairs with even keys using dictionary comprehension.
- Initialize a variable x to 0 for storing the XOR of values of even keys.
- Loop through the values in the even_dict and calculate the XOR with the current value of x.
- Print the final result of x as XOR of values for even keys
Python3
my_dict = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 }
even_dict = {key: my_dict[key] for key in my_dict if key % 2 = = 0 }
Gfgresult = 0
for value in even_dict.values():
Gfgresult ^ = value
print (Gfgresult)
|
Time Complexity: O(N), where N is the number of keys in the dictionary,
Space Complexity: O(N) because we are creating a new dictionary,the max value can be N if all are even
Method 5: Using list comprehension
- Define a dictionary.
- Use list comprehension to create a list of values corresponding to even keys.
- Use the reduce() function from the functools module to perform the XOR operation on the list.
- Print the XOR result.
Python3
import functools
my_dict = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 }
even_values = [my_dict[key] for key in my_dict if key % 2 = = 0 ]
Gfgresult = functools. reduce ( lambda x, y: x ^ y, even_values)
print (Gfgresult)
|
Time complexity: O(n)
Auxiliary space: O(n)
Method 6: Use a dictionary comprehension + XOR operator
- Creates a new dictionary even_dict with only the even keys and their values from the original dictionary my_dict.
- It then performs the XOR operation on the values in even_dict using a for loop and the XOR operator ^.
- Finally, it prints the result.
Python3
my_dict = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 }
even_dict = {key: my_dict[key] for key in my_dict if key % 2 = = 0 }
Gfgresult = 0
for value in even_dict.values():
Gfgresult ^ = value
print (Gfgresult)
|
Time complexity: O(n)
Auxiliary space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...