The Shining: typewriter scripting

I wanted to write a simple script to mimic a typewriter typing The Shining’s “All work and no play” lines. I wrote it in python first, but then decided to re-write it in JavaScript. The Python is more complex than it needs to be, but I wanted to practice generators.

All work and no play 

Python:

  1. #!/usr/bin/env python3
  2. import time
  3. import random
  4.  
  5. jack = "All work and no play makes Nick a dull boy. "
  6. PROB_SKIP = 0.01
  7.  
  8. def sometimes(percent=0.50):
  9. return random.random() < percent
  10.  
  11. def all_work():
  12. while True:
  13. for c in jack:
  14. if sometimes(PROB_SKIP):
  15. continue
  16. yield c
  17. time.sleep(0.050)
  18. time.sleep(0.400)
  19.  
  20. if __name__ == '__main__':
  21. for work in all_work():
  22. print("{work}".format(work=work), end=" ", flush=True)

Javascript:

  1. function typer(text, n) {
  2. if (n < (text.length)) {
  3. document.getElementById("typer").innerHTML = text.substring(0, n++);
  4. setTimeout(function() { typer(text, n) }, 100);
  5. }
  6. }