Skip to content

Instantly share code, notes, and snippets.

@auscompgeek
Last active November 27, 2017 21:47
Show Gist options
  • Save auscompgeek/6203547 to your computer and use it in GitHub Desktop.
Save auscompgeek/6203547 to your computer and use it in GitHub Desktop.
anagram generator written in Python
#!/usr/bin/python3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
def get_sword(word):
return "".join(sorted(word.replace("'", "").lower()))
def get_words(words="/usr/share/dict/words"):
words = open(words)
for word in words:
word = word.strip()
sword = get_sword(word)
if not sword in wordmap:
wordmap[sword] = []
wordmap[sword].append(word)
def main():
word = input("Anagram letters: ")
while word:
sword = get_sword(word)
if sword in wordmap:
print("Anagrams:", ", ".join(wordmap[sword]))
else:
print("No anagrams.")
word = input("Anagram letters: ")
wordmap = {}
get_words()
if __name__ == '__main__':
main()
@quintendewilde
Copy link

should there be words in the wordmap? I'm trying this but I get no anagrams.

#########################################################################
def get_sword(word):
    return "".join(sorted(word.replace("'", "").lower()))


def get_words(words="/usr/share/dict/words"):
    words = open(words)

    for word in words:
        word = word.strip()

        sword = get_sword(word)

        if not sword in wordmap:
            wordmap[sword] = []

        wordmap[sword].append(word)


def main():
    word = (fake.romanized_name())
    while word:
        sword = get_sword(word)

        if sword in wordmap:
            print("Anagrams:", ", ".join(wordmap[sword]))
        else:
            print("No anagrams.")

        


wordmap = {}

get_words()

if __name__ == '__main__':
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment