Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import blist
- import collections
- import heapq
- import itertools
- import operator
- import sys
- import timeit
- def least_counter_heap(article):
- wordcounter = collections.Counter(article.split())
- wordlist = [(wordcount, word) for (word, wordcount) in wordcounter.items()]
- heapq.heapify(wordlist)
- while True:
- wordcount, word = heapq.heappop(wordlist)
- if wordcount >= 5:
- return
- yield (word, wordcount)
- def least_counter_sort(article):
- wordcounter = collections.Counter(article.split())
- allwords = sorted(wordcounter.items(), key=operator.itemgetter(1))
- return itertools.takewhile(lambda x: x[1] < 5, allwords)
- if sys.version_info >= (3, 0):
- with open(sys.argv[1], errors='ignore') as f:
- article = f.read()
- else:
- with open(sys.argv[1]) as f:
- article = f.read()
- assert sorted(least_counter_heap(article)) == sorted(least_counter_sort(article))
- def lch():
- collections.deque(least_counter_heap(article), maxlen=0)
- def lcs():
- collections.deque(least_counter_sort(article), maxlen=0)
- print('heap: {}'.format(timeit.timeit(lch, number=10)))
- print('sort: {}'.format(timeit.timeit(lcs, number=10)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement