Finding words that start/end with input letters (tiles) from the user in a file [closed]

StackOverflow https://stackoverflow.com/questions/20531502

  •  31-08-2022
  •  | 
  •  

質問

So I've been trying to implement a Scrabble word game, and part of that is for me to be able to find words that start and end with one or more letters (or tiles since this is scrabble) entered by a user. There is a words.txt file that contains all of the words.

Really have no idea how to implement it with more than one letter. I'd prefer a loop that reads through the file line by line and then checks with the users input. But other function based answers are fine too!

Thanks!

P.S: It's my first post here so I apologize if I did not ask correctly

役に立ちましたか?

解決

You can try the following solution. The user has to enter some letters and the program looks, whether those letters can be found at the beginning or end of a word.

with open('words.txt') as f:
    wordlist = [line.strip().lower() for line in f]

from itertools import permutations
while True:
    userinput = raw_input("Please enter your letters: ").lower()
    # look up any combinations of letters
    for letter_permutation in permutations(userinput):
        s = ''.join(letter_permutation)
        for word in wordlist:
            if word.startswith(s) or word.endswith(s):
                print "'%s' is part of %s" % (s, word)
    if raw_input('Press q to exit, any key to continue... ').lower() == 'q':
        break

For small length of userinput, you can also create a tuple with all permutations, but since its size would increase with the factorial of the length, I would suggest to use the solution above. But for the sake of showing cool python stuff. (Thanks to Jon Clements for advise to use str.startswith or str.endswith with tuples)

while True:
    userinput = raw_input("Please enter your letters: ").lower()
    # create a tuple with all combinations of letters
    perms = tuple( ''.join(letter_permutation)
                   for letter_permutation in permutations(userinput) )
    # now check if any of the words starts or ends with any of the letter-perms
    for word in wordlist:
        # startswith, endswith also take tuples as arguments
        if word.startswith(perms) or word.endswith(perms):
            print "'%s' can be found in %s" % (userinput, word)

    if raw_input('Press q to exit, any key to continue... ').lower() == 'q':
        break

他のヒント

Not really sure what you're trying to accomplish, can you give more detail by editing your post?

I'm trying to extrapolate that you're checking the played word against a wordlist you have somewhere? No need to check words that start with one or more letters, just read the whole word into a variable and..

word = readTheWord() #read the word into the variable
if word in wordlist: #for some wordlist dictionary
  #accept the input
else:
  #deny the input

Is this what you're trying to accomplish?

Which part are you stuck on?

Google a tutorial on how to read a file line-by-line, if you haven't got that far yet.

Make sure the file is sorted alphabetically. If there aren't too many words, load them all into memory at the start of the program. Do a binary search with your input string. If there's no exact match, it should give you back an index where the word would fit. From there, scan forwards and backwards to find all words starting with your input string. This should take O(n log n) time instead of linear time.

Make a copy of the word list, reverse every string, including your input string, and then repeat the above to find words ending with your input string.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top