Challenge: Count with Dictionary
Count occurrences with the dictionary.
We'll cover the following...
Problem statement
In this challenge, write a function count_letters(message) that counts occurrences of each letter in message using the defaultdict type dictionary, and then sorts the items on the basis of values using OrderedDict.
Input
A string
Output
A dictionary of type OrderedDict
Sample input
'Welcome to Educative'
Sample output
OrderedDict([('d', 1), ('a', 1), ('i', 1), ('l', 1), ('v', 1), ('W', 1), ('m', 1), ('u', 1), ('E', 1), ('c', 2), ('t', 2), (' ', 2), ('o', 2), ('e', 3)])
Try to implement the function below. Don’t forget to use the concepts taught in the previous lessons. Feel free to view the solution, after giving it a few shots. Good luck!
from collections import defaultdictfrom collections import OrderedDictdef count_letters(message):# Begin writing your implementation herereturn None
We hope that you were able to solve the challenge.
Ask