Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified version of your look_w function #1

Merged
merged 3 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)