Random Letter Frequency Analysis

  • Share this:

Code introduction


This function takes 10 arguments, generates a string of random lowercase letters, counts the occurrences of each letter, and returns the top five letters and their counts.


Technology Stack : collections.Counter, string, typing, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
def a_to_z(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10):
    from collections import Counter
    import string
    from typing import List
    import random

    # Generate a list of lowercase letters
    letters = list(string.ascii_lowercase)
    # Shuffle the list
    random.shuffle(letters)
    # Join the shuffled letters to form a string
    shuffled_string = ''.join(letters)
    
    # Count occurrences of each letter
    letter_count = Counter(shuffled_string)
    
    # Sort the letters by occurrence and return the top 5
    top_five = sorted(letter_count.items(), key=lambda x: x[1], reverse=True)[:5]
    
    # Return the sorted list of tuples
    return top_five