Skip to content

Instantly share code, notes, and snippets.

@umanomata
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umanomata/1dbb3beb2ce19f09fcee to your computer and use it in GitHub Desktop.
Save umanomata/1dbb3beb2ce19f09fcee to your computer and use it in GitHub Desktop.
# Program based on example code from composingprograms.com
#
# - Get a text document from the Web containing works of Shakespeare.
# - Count the words in the document and display the result.
# - Display the list of words that are six-letters long and that written
# backwards form a word that is also included in the document.
#
# NOTE: This code takes about 6-8 seconds to run in my machine.
#
# Also note that the list of words obtained with split in this case does
# not include words only but also punctuation marks (the original code
# from the book does the same).
from urllib.request import urlopen
shakespeare = urlopen('http://composingprograms.com/shakespeare.txt')
words = set(shakespeare.read().decode().split())
print(len(words))
for w in words:
if len(w) == 6 and w[::-1] in words:
print(w)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment