Last active
November 27, 2017 21:47
-
-
Save auscompgeek/6203547 to your computer and use it in GitHub Desktop.
anagram generator written in Python
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
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should there be words in the wordmap? I'm trying this but I get no anagrams.