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
# Function to check if the element is even def isEven(x): if x % 2 = = 0 : return True return False # Function to find the result def xorOfDictionaryKeys(dic): # Variable to store result result = 0 # Traversing the dictionary for i in dic: # Check if the current key is even if isEven(i): result = result ^ dic[i] return result dic = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 } print (xorOfDictionaryKeys(dic)) |
2
Time complexity: O(n)
Auxiliary Space: O(1)
Method 2: Using items() function
Python3
# Function to check if the element is even def isEven(x): if x % 2 = = 0 : return True return False # Function to find the result def xorOfDictionaryKeys(dic): # Variable to store result result = 0 # Traversing the dictionary for key, value in dic.items(): # Check if the current key is even if isEven(key): result = result ^ value return result dic = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 } print (xorOfDictionaryKeys(dic)) |
2
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
# Define a dictionary my_dict = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 } # Use the filter() function even_values = filter ( lambda kv: kv[ 0 ] % 2 = = 0 , my_dict.items()) # Initialize a variable to store the XOR result Gfgresult = 0 # Use a for loop to calculate the XOR of values of all even keys for key, value in even_values: Gfgresult ^ = value # Print the XOR result print (Gfgresult) |
2
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
# Define a dictionary my_dict = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 } # Use the dictionary comprehension even_dict = {key: my_dict[key] for key in my_dict if key % 2 = = 0 } # Initialize a variable to store the XOR result Gfgresult = 0 # Use a for loop to calculate the XOR of values of all even keys for value in even_dict.values(): Gfgresult ^ = value # Print the XOR result print (Gfgresult) |
2
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 module import functools # Define a dictionary my_dict = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 } # Use list comprehension to create a list of values corresponding to even keys even_values = [my_dict[key] for key in my_dict if key % 2 = = 0 ] # Use the reduce() function to perform XOR operation on the list Gfgresult = functools. reduce ( lambda x, y: x ^ y, even_values) # Print the XOR result print (Gfgresult) |
2
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
# Define a dictionary my_dict = { 1 : 3 , 4 : 5 , 6 : 7 , 3 : 8 } # Use a dictionary comprehension to create a new dictionary # with even keys and their values even_dict = {key: my_dict[key] for key in my_dict if key % 2 = = 0 } # Use the XOR operator to perform the XOR operation # on the values in the even_dict Gfgresult = 0 for value in even_dict.values(): Gfgresult ^ = value # Print the XOR result print (Gfgresult) |
2
Time complexity: O(n)
Auxiliary space: O(n)