Skip to content

Commit

Permalink
Merge pull request #1 from Stack-of-Pancakes/master
Browse files Browse the repository at this point in the history
Simplified version of your look_w function
  • Loading branch information
kakkarja authored May 2, 2018
2 parents 1bf0341 + c0fba76 commit 13ef74f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 16 additions & 0 deletions look_w.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Open words.txt. Assumes words.txt is in same folder as look_w.py
words = open('words.txt').read().split()


def look_w(word, num):
# Looking words that in the words list
# by number of letters and alphabets
return [w for w in words if len(w) == num and
all(w.lower().count(c) <= word.lower().count(c) for c in w.lower())]


# Usage
print(look_w('insane', 6)) # prints ['inanes', 'insane', 'sienna']
print(look_w('quit', 3)) # prints ['ITU', 'qui', 'Tiu', 'tui', 'UIT', 'uti']
print(look_w('quit', 4)) # prints ['quit']
print(look_w('cake', 99)) # prints []
17 changes: 6 additions & 11 deletions read_english_dictionary.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import json
import os, sys

def load_words():
try:
filename = os.path.dirname(sys.argv[0])+"\\"+"words_dictionary.json"
with open(filename,"r") as english_dictionary:
valid_words = json.load(english_dictionary)
return valid_words
except Exception as e:
return str(e)
with open('words_alpha.txt') as word_file:
valid_words = set(word_file.read().split())

return valid_words


if __name__ == '__main__':
english_words = load_words()
# demo print
print(english_words["fate"])
print('fate' in english_words)

0 comments on commit 13ef74f

Please sign in to comment.