Advertisement
Guest User

Untitled

a guest
Jan 30th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import blist
  4. import collections
  5. import heapq
  6. import itertools
  7. import operator
  8. import sys
  9. import timeit
  10.  
  11. def least_counter_heap(article):
  12.     wordcounter = collections.Counter(article.split())
  13.     wordlist = [(wordcount, word) for (word, wordcount) in wordcounter.items()]
  14.     heapq.heapify(wordlist)
  15.     while True:
  16.         wordcount, word = heapq.heappop(wordlist)
  17.         if wordcount >= 5:
  18.             return
  19.         yield (word, wordcount)
  20.  
  21. def least_counter_sort(article):
  22.     wordcounter = collections.Counter(article.split())
  23.     allwords = sorted(wordcounter.items(), key=operator.itemgetter(1))
  24.     return itertools.takewhile(lambda x: x[1] < 5, allwords)
  25.  
  26. if sys.version_info >= (3, 0):
  27.     with open(sys.argv[1], errors='ignore') as f:
  28.         article = f.read()
  29. else:
  30.     with open(sys.argv[1]) as f:
  31.         article = f.read()
  32. assert sorted(least_counter_heap(article)) == sorted(least_counter_sort(article))
  33. def lch():
  34.     collections.deque(least_counter_heap(article), maxlen=0)
  35. def lcs():
  36.     collections.deque(least_counter_sort(article), maxlen=0)
  37. print('heap: {}'.format(timeit.timeit(lch, number=10)))
  38. print('sort: {}'.format(timeit.timeit(lcs, number=10)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement