Skip to content

Instantly share code, notes, and snippets.

@pansapiens
Last active June 22, 2017 09:51
Show Gist options
  • Save pansapiens/e09aa63b9b5d6d11074c513b60216cd8 to your computer and use it in GitHub Desktop.
Save pansapiens/e09aa63b9b5d6d11074c513b60216cd8 to your computer and use it in GitHub Desktop.
Finds all English dictionary words that are the same when you move the first letter to the end and then reverse it
import requests
from cashier import cache
"""
TO ALL MY INTELLIGENT FRIENDS: WHAT DO THESE 7 WORDS HAVE IN COMMON?
I am sending this only to my smart friends. I could not figure it out. See if you can figure out what these seven words all have in common
1. Banana
2. Dresser
3. Grammar
4. Potato
5. Revive
6. Uneven
7. Assess
Are you peeking or have you already given up?
Give it another try....
Look at each word carefully. You'll kick yourself when you discover the answer. This is so cool.....
No, it is not that they all have at least 2 double letters.
Let me know if you work it out - I didn't!
Answer is below!
Answer:
In all of the words listed, if you take the first letter, place it at the end of the word, and then spell the word backwards, it will be the same word. Did you figure it out? No?
Then send this to more people and stump them as well.
Then, you'll feel better too.
(In other words, remove the first letter and you have a pallendrome or spika.]
"""
@cache()
def get_url_lines(url="https://raw.githubusercontent.com/jonbcard/scrabble-bot/master/src/dictionary.txt"):
text = requests.get(url).text
return text.splitlines()
def remove_short_words(words, n=2):
for w in words:
if len(w) > n:
yield w
def remove_dotted_words(words):
for w in words:
if '.' not in w:
yield w
def remove_allcaps_words(words):
for w in words:
if w == w.upper():
continue
yield w
def remove_proper_nouns(words):
for w in words:
if w[0].islower():
yield w
def circpermrev(w):
# move last letter to end, then reverse everything
# banana -> banana
return (w[1:] + w[0])[::-1]
def find_circpermrevs(words):
for word in words:
lowercase = word.lower()
if lowercase == circpermrev(lowercase):
yield word
if __name__ == "__main__":
words = set()
# We grab a bunch of English words dictionaries, remove duplicate words.
#words.update(get_url_lines("https://raw.githubusercontent.com/dwyl/english-words/master/words.txt"))
#words.update(get_url_lines("https://raw.githubusercontent.com/jonbcard/scrabble-bot/master/src/dictionary.txt"))
#words.update(get_url_lines("http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt"))
# Just valid Scrabble words
words = get_url_lines("https://raw.githubusercontent.com/jonbcard/scrabble-bot/master/src/dictionary.txt")
words = \
sorted(
# remove_proper_nouns(
# remove_allcaps_words(
remove_dotted_words(
remove_short_words(
words))) # ) #)
for n, w in enumerate(find_circpermrevs(words)):
print("%s. %s" % (n+1, w))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment