From 463ed294c42e3c1891476ed2e2e7c5c1c68b106a Mon Sep 17 00:00:00 2001 From: Gourav Date: Fri, 2 Oct 2020 12:28:59 +0530 Subject: [PATCH] Using dictionary to count the frequency of letters in the input string closes #150 --- Python/count_letter.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Python/count_letter.py diff --git a/Python/count_letter.py b/Python/count_letter.py new file mode 100644 index 0000000..56479b1 --- /dev/null +++ b/Python/count_letter.py @@ -0,0 +1,22 @@ +def count_letters(text): + result = {} + # Go through each letter in the text + count = 0 + for letter in text.lower(): + # Check if the letter needs to be counted or not + if letter.isalpha(): + if letter not in result: + result[letter]=0 + result[letter]+=1 + # Add or increment the value in the dictionary + + return result + +print(count_letters("AaBbCc")) +# output {'a': 2, 'b': 2, 'c': 2} + +print(count_letters("Math is fun! 2+2=4")) +# output {'m': 1, 'a': 1, 't': 1, 'h': 1, 'i': 1, 's': 1, 'f': 1, 'u': 1, 'n': 1} + +print(count_letters("This is a sentence.")) +# output {'t': 2, 'h': 1, 'i': 2, 's': 3, 'a': 1, 'e': 3, 'n': 2, 'c': 1} \ No newline at end of file