Guest User

Untitled

a guest
Mar 1st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import io
  2.  
  3.  
  4. def iter_words(stream, bufsize=32):
  5. if not isinstance(stream, io.IOBase):
  6. stream = io.StringIO(stream)
  7. word = ""
  8.  
  9. while True:
  10. buf = stream.read(bufsize)
  11. if not buf:
  12. if word:
  13. yield word
  14. break
  15. for ch in buf:
  16. if ch == " ":
  17. if word:
  18. yield word
  19. word = ""
  20. else:
  21. word += ch
  22.  
  23.  
  24. def freq(s, bufsize):
  25. words = {}
  26. for word in iter_words(s, bufsize):
  27. if word in words:
  28. words[word] += 1
  29. else:
  30. words[word] = 1
  31. words = list(words.items())
  32. words.sort(key=lambda el: el[1])
  33. return words
  34.  
  35.  
  36. chars = "qwertyuiopasdfghjklzxcvbnm"
  37. words = [c * i for c in chars for i in range(1, 20)]
  38. data = " q " + "%s " % " ".join(words) * 10
  39.  
  40.  
  41. if __name__ == "__main__":
  42. res = freq(data, 64)
  43. print(res)
  44. assert res[-1][0] == "q"
Add Comment
Please, Sign In to add comment