forked from dwyl/english-words
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Stack-of-Pancakes/master
Simplified version of your look_w function
- Loading branch information
Showing
2 changed files
with
22 additions
and
11 deletions.
There are no files selected for viewing
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
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 [] |
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
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) |