A-to-Z Frequency Converter

  • Share this:

Code introduction


Converts the input string to uppercase and counts the five most frequently occurring letters.


Technology Stack : string, collections, heapq

Code Type : Function

Code Difficulty : Intermediate


                
                    
def a_to_z_converter(input_string):
    import string
    from collections import Counter
    import heapq

    def get_frequent_letters(s):
        letter_counts = Counter(s)
        return heapq.nlargest(5, letter_counts.items(), key=lambda x: x[1])

    converted_string = ''.join([char if char in string.ascii_lowercase else ' ' for char in input_string])
    frequent_letters = get_frequent_letters(converted_string)

    return frequent_letters