Letter Occurrence Counter

  • Share this:

Code introduction


Calculates the occurrence count of each letter (a-z) in the given text.


Technology Stack : collections, string

Code Type : Function

Code Difficulty : Intermediate


                
                    
def a_to_z_counter(text):
    def is_alpha(c):
        return c.isalpha()

    def get_letter_count(c):
        return text.count(c)

    from collections import Counter
    from string import ascii_lowercase
    import string

    counter = Counter(text)
    letter_counts = {letter: counter[letter] for letter in ascii_lowercase if is_alpha(letter)}
    letter_counts = {letter: get_letter_count(letter) for letter in ascii_lowercase if letter in counter}

    return letter_counts