patternpythonMinor
Finding words of a given length only using given letters
Viewed 0 times
lengthwordsusingonlyfindinggivenletters
Problem
It's a (simple) guesser for game where you need to find words of a given length only using given letters. I'd like to know if there's a more pythonic way to do things here.
#!/usr/bin/env python3
import sys
if len(sys.argv) < 3:
sys.exit("Usage: " + sys.argv[0] + " letters number_of_letters [dictionnaryfile]")
letters = sys.argv[1].upper()
wordsize = int(sys.argv[2])
dictionnary_name = "dict.txt" if len(sys.argv) < 4 else sys.argv[3]
try:
wordlist = open(dictionnary_name).read().split('\n')
except FileNotFoundError:
sys.exit("Couldn't find dictionnary file \"" + dictionnary_name + "\"")
good = []
for w in wordlist:
if len(w) == wordsize:
up = w.upper()
ok = True
for c in up:
if not c in letters:
ok = False
break
if ok:
good.append(w)
print("Found " + str(len(good)) + " results:")
print(good)Solution
The following code shows I would have done what you asked in Python 2.7. Other readers will tell you the ways in which it is a) not Pythonic and b) wrong.
-
Start with a module comment describing what the code is intended to do. (This is available to the code as
-
Use the various Python string delimiters to avoid escaping.
-
Name variables consistently.
-
strip() text strings read from a text file.
-
Make letters a set to reflect its usage.
-
Create the list of matching words with a list comprehension.
Here is the code.
-
Start with a module comment describing what the code is intended to do. (This is available to the code as
__doc__.)-
Use the various Python string delimiters to avoid escaping.
-
Name variables consistently.
-
strip() text strings read from a text file.
-
Make letters a set to reflect its usage.
-
Create the list of matching words with a list comprehension.
Here is the code.
"""
Find all the words in a reference dictionary with a specified number of letters
that contain all the letters in a specified list of letters.
The number of letters and the list of letters must be specified on the command line.
The reference dictionary may be specified on the command line. If is not then
dict.txt is used.
The matching of letters is case-insensitive.
"""
import sys
if len(sys.argv) > sys.stderr, __doc__
sys.exit('Usage: %s letters number_of_letters [dictionary_file]' % sys.argv[0])
letters = set(sys.argv[1].upper())
wordsize = int(sys.argv[2])
dictname = 'dict.txt' if len(sys.argv) <= 3 else sys.argv[3]
try:
wordlist = [w.strip() for w in open(dictname).readlines()]
except IOError:
sys.exit('''Couldn't find dictionary file "%s"''' % dictname)
matches = [w for w in wordlist
if len(w) == wordsize
and all(c in letters for c in w.upper())]
print 'Found %d results:' % len(matches)
print matchesCode Snippets
"""
Find all the words in a reference dictionary with a specified number of letters
that contain all the letters in a specified list of letters.
The number of letters and the list of letters must be specified on the command line.
The reference dictionary may be specified on the command line. If is not then
dict.txt is used.
The matching of letters is case-insensitive.
"""
import sys
if len(sys.argv) < 3:
print >> sys.stderr, __doc__
sys.exit('Usage: %s letters number_of_letters [dictionary_file]' % sys.argv[0])
letters = set(sys.argv[1].upper())
wordsize = int(sys.argv[2])
dictname = 'dict.txt' if len(sys.argv) <= 3 else sys.argv[3]
try:
wordlist = [w.strip() for w in open(dictname).readlines()]
except IOError:
sys.exit('''Couldn't find dictionary file "%s"''' % dictname)
matches = [w for w in wordlist
if len(w) == wordsize
and all(c in letters for c in w.upper())]
print 'Found %d results:' % len(matches)
print matchesContext
StackExchange Code Review Q#26857, answer score: 8
Revisions (0)
No revisions yet.