Guest User

Untitled

a guest
Oct 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. from subprocess import check_output
  2.  
  3. def get_zen():
  4. """Capture the output of `import this` and return it."""
  5. return check_output(['python', '-m', 'this'])[:-1] # remove the last newline
  6.  
  7. ZEN = get_zen()
  8. ZENLIST = ZEN.split('\n')[2:] # Remove the first two lines (title and empty)
  9.  
  10. def find_zen(s):
  11. """Find a sentence in the zen that correponds to a given string."""
  12. s = s.lower()
  13.  
  14. # first, look for a unique sentence that contains the whole string
  15. l = [line for line in ZENLIST if s in line.lower()]
  16. if len(l) == 1:
  17. return l[0]
  18.  
  19. # second, look for a line that contains all the words in the string
  20. l = [line for line in ZENLIST if all(word in line.lower() for word in s.split())]
  21. if len(l) == 1:
  22. return l[0]
  23.  
  24. return None
Add Comment
Please, Sign In to add comment