
Python program to count Even and Odd numbers in a Dictionary
Given a python dictionary, the task si to count even and odd numbers present in the dictionary.
Examples:
Input : {‘a’: 1, ‘b’: 2, ‘c’: 3, ‘d’: 4, ‘e’ : 5}
Output : Even = 2, odd = 3Input : {‘x’: 4, ‘y’:9, ‘z’:16}
Output : Even = 2, odd = 1
Approach using values() Function: Traverse the dictionary and extract its elements using values() function and for each extracted value, check if it is even or odd. Finally, print the respective counts.
Python3
|
Even Count: 2 Odd Count: 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Alternate Approach: Iterate over each item in the dictionary, and for each element, check if it is even or odd. Finally, print the respective counts.
Python3
|
Even count: 2 Odd count: 3
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
0 comments:
Post a Comment