Python practice challenge: Find 3 most frequently used words in a text

in #python4 years ago

I just thought that it may be interesting for those who learn Python to make some sort of competition, combining the practice in python language and reward in Hive :)



image source

So, the conditions are following: just be the first to solve the following task and get 50% of liquid reward for this post :)

And here comes the task!

Description


To write a function that will accept some text as input (that can have punctuation and line breaks) and return in the form of array of 3 most frequently used words

Asssumptions

  • A word in text can have one or more appostrophes (')
  • Words in text are strings that consists from letters (A to Z) and any other characters (like #, \, / , . ...) are considered to be not part of a word and should be treated as whitespace
  • Case should not influence on result ( for example, "Home" and "home" are considered to be the same word)
  • If there will be less then 3 unique words in the text then the result should either 1-2 words or empty array
  • Examples:



    text = "e e e e DDD ddd DdD: ddd ddd aa aA Aa, bb cc cC e e e"
    correct answer = ['e', 'ddd', 'aa']




    In 7 day we will get winner, I will post my solution and if you like this post we will continue with another task :) Looking forward for your comments!


    text = "M e r r y M a r y M a r r y M e" correct answer = ['r' , 'm', 'y'] text="This is a conversation between a Canadian man and a US ship from October 1995./nIt happened near the Canadian sea coast. Canadian man: You have to change your direction./nIf you don’t change your direction, there will be a big collision with us./nUS ship: We recommend that you change your direction./nCanadian man: It is not possible. You have to change your direction./nUS ship: This is the captain of a US ship. You have to change your direction./nCanadian man: It is not possible. We repeat. You have to change your direction./nUS ship: This is a US ship. We have a lot of weapons. We repeat. You have to change your direction./nCanadian man: This is a lighthouse. We are waiting for your answer." correct answer = "['a', 'your', 'you']"
    Sort:  

    Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

    Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

    You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.