logo CodeStepByStep logo


Language/Type: Python file input

Write a function named most_common that accepts a string parameter representing a file name, then opens/reads that file's contents, and returns the word that occurs most frequently in the file. Words are separated by one or more spaces. Assume that any duplicate words will occur consecutively. If there is a tie, use the earlier word. You may assume that the string contains at least one word. For example, the following file's data will cause your function to return "Kim" because the word Kim occurs 3 times:

Ben Eric  Eric Marty Kim
Kim Kim  Jenny   Nancy
Nancy  Nancy  Paul   Paul
Bare code: Write a fragment of Python code as described, without any class or function/method heading around your code.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.