Word Searcher from Words.list

143 Views Asked by At

I was helping my mom with this word search app earlier while taking a break from some python tutorials and I realized -- what a perfect example of something interesting for me to work on/learn from.

Here's what I came up with, and while it works, I have to do some extra steps to figure out the words.

It seems that my double characters don't register as two, and one of them gets replaced with a non-relevant letter.

I've tried a lot of different things -- popping, removing. for character in words and not in myletters (to subtract out the difference). A lot of what I found involving this stuff were grids, and directionals, but I'm really just interested in a split list of words from my character input. Any help is appreciated.

Sidenote - I am very new to coding, so idk if this was the appropriate route. This x was x * 10 simpler than any example I could find. (Maybe why it doesn't work the way I want it to? ;p)

wordlist = open("PossibleWords.txt", "r").read().split()
myletters = input("Letters?: ").lower()
s = list()
sorted = str(myletters)

for word in wordlist:
    if len(word) == len(myletters) and all(letter in word for letter in sorted):
        s.append(word)
for length in s[:100]:
print(length)
1

There are 1 best solutions below

3
On BEST ANSWER

Based on the OP's recent comment, it sounds like we're taking about word search. To not lose the double letters, we'll use Counter and compare counters:

from collections import Counter

def is_sub_count(counter_super, counter_sub):

    counter_sub.subtract(counter_super)

    return all(count <= 0 for count in counter_sub.values())

myLetters = input("Letters?: ").lower()
myCount = Counter(myLetters)
myLength = len(myLetters)

words = open("PossibleWords.txt").read().split()

found = list()

for word in words:
    if len(word) <= myLength and is_sub_count(myCount, Counter(word.lower())):
        found.append(word)

print(*found, sep='\n')

USAGE

> python3 test.py
Letters?: uassage
a
age
ague
as
ass
assuage
gas
gauss
guess
sag
saga
sage
sausage
sea
sue
us
usage
use
>