Last active
August 29, 2015 14:27
-
-
Save umanomata/1dbb3beb2ce19f09fcee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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