Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io
- def iter_words(stream, bufsize=32):
- if not isinstance(stream, io.IOBase):
- stream = io.StringIO(stream)
- word = ""
- while True:
- buf = stream.read(bufsize)
- if not buf:
- if word:
- yield word
- break
- for ch in buf:
- if ch == " ":
- if word:
- yield word
- word = ""
- else:
- word += ch
- def freq(s, bufsize):
- words = {}
- for word in iter_words(s, bufsize):
- if word in words:
- words[word] += 1
- else:
- words[word] = 1
- words = list(words.items())
- words.sort(key=lambda el: el[1])
- return words
- chars = "qwertyuiopasdfghjklzxcvbnm"
- words = [c * i for c in chars for i in range(1, 20)]
- data = " q " + "%s " % " ".join(words) * 10
- if __name__ == "__main__":
- res = freq(data, 64)
- print(res)
- assert res[-1][0] == "q"
Add Comment
Please, Sign In to add comment