Skip to content

Instantly share code, notes, and snippets.

@mreider
Created November 22, 2015 05:49
Show Gist options
  • Select an option

  • Save mreider/581c533306e80020dd35 to your computer and use it in GitHub Desktop.

Select an option

Save mreider/581c533306e80020dd35 to your computer and use it in GitHub Desktop.
import string
def analyze(file):
count = {}
remove = dict.fromkeys(map(ord, string.punctuation))
f = open(file ,'r')
f = f.read().lower()
f = f.translate(remove)
wordList = f.split()
for word in wordList:
if not count.get(word,None):
count[word] = 1
else:
count[word] += 1
totals = {'mostCommonWords':sorted(count, key=count.get, reverse=True), \
'leastCommonWords' : sorted(count, key=count.get), \
'shortestWord' : min(wordList, key=len), \
'longestWord' : max(wordList, key=len) }
return totals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment