Top 3 most frequently occurring words in a string (python)
Question: Please no importing counters. I need to write a function that takes out the
top 3 most occurring words in a string and returns them in a list in the order
of most frequently occurring to least frequently occurring.

so `h("the the the the cat cat cat in in hat ")`

    
    
    >>> ["the", "cat", "in"]
    

and if there are less than 3 types of words in a string:

    
    
    h("the the cat")
    >>> ["the", "cat"]
    


Answer: The frequency hash is first populated with the number of times each word
appears in a given string. The top 3 words are then determined based on the
counts from the frequency hash.

**Code**

    
    
    def h(string):
        return get_top_3(get_frequency_hash(string))
    
    def get_frequency_hash(text):
        array = text.split(" ")
        frequency = {} 
        for word in array: 
            try: 
               frequency[word] += 1 
            except: 
               frequency[word]= 1
        return frequency
    
    def get_top_3(frequency_hash):
        array_of_tuples = [(k,v) for k,v in frequency_hash.items()]
        sorted_array_of_tuples = sorted(array_of_tuples, key=lambda x: -x[1])
        return [k for k,v in sorted_array_of_tuples[0:3]]
    

**Example**

    
    
    h("the the the the cat cat cat in in hat")
    # ['the', 'cat', 'in']
    

