5 Replies - 796 Views - Last Post: 09 July 2020 - 09:00 AM Rate Topic: -----

#1 DB9323   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 18-November 17

Short script to help solve word puzzle game

Posted 09 July 2020 - 06:48 AM

Hi,

I've been playing a game on my phone called wordtrip, The basic idea is that you are given a set of increasingly many characters at the start of each round. Your goal, as the player, is to pick out as many valid ( actual English ) words as you can. So you might a round with the following letters, scout... you'll see and get credit for inputing as many valid combinations of those letters as possible. Words can be of various legths -= but you cannot repeat a letter twice ( unless it is included in the original set ). So with scout you would be entering 'scout', 'cost', 'out', 'cot'.. etc.

So my idea is to come up with a script that does the work for you. Yes, it's cheating - but this is just a little demo program I'm working on to maintain my Python skills and I'm not training for the next Olympic spelling Bee.

I have the script pretty much working except I have noticed a couple edge cases. For example, when given the letters 'writs', it does not return a match on wrist as would be expected. I'm asumuing this is because the initial string given is 'wrist'.

This is NOT homework. I was just hoping someone else might find the task interesting and would be willing to help me out and/or critique my approach. It's pretty brute force ( so it's slow for greater lengths ) but I don't have a whole lot of algorithms experience yet.A few suggestions on how to optimize something like this would be appreciated.

from itertools import permutations

ALL_WORDS = []

def get_user_input():
    char_set = input('Please enter the desired character set:  (i.e. hope  ')
    resultant_word_length = int(input("Enter the size of word(s) you are looking for: "))
    return char_set, resultant_word_length


def load_dictionary(filename):
    with open('corncob_lowercase.txt', 'r') as file:
        for line in file:
            for word in line.split():
                ALL_WORDS.append(word)
        return ALL_WORDS


def get_good_words(possible_permutations):
    good_words = []
    for word in ALL_WORDS:
        for combination in possible_permutations:
            if word == combination:
                good_words.append(word)

    return good_words

def check_words(possible_permutations):
    checked_words = any(ALL_WORDS)
    return checked_words


def main():
    #  load the dictionary into memory
    ALL_WORDS = load_dictionary('corncob_lowercase.txt')

    #  get character string fronm the user
    character_string, result_length = get_user_input()

    for i in range(1, result_length):
        possible_permutations = ([''.join(p) for p in permutations(character_string, i)])

    print("Possible X Letter Permutations: ")
    print(possible_permutations)
    print("Matching words: ")

    good_words = get_good_words(possible_permutations)
    #  take care to append the initial permutation
    if ALL_WORDS.count(character_string) == 1:
        #  if the permutation is present in dictionary ALL_WORDS
        #  then append it to the list of good words
        good_words.append(character_string)
    print(good_words)


main()



Here's a link where y7ou can view the actual game this is intended to accompay: https://www.youtube....9&v=Xj_Q2ZtgM-c

If posting links like this is discouraged, please let me know and I will delete.

Thanks for your help,
Marc

Is This A Good Question/Topic? 1
  • +

Replies To: Short script to help solve word puzzle game

#2 DB9323   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 18-November 17

Re: Short script to help solve word puzzle game

Posted 09 July 2020 - 06:54 AM

I just wanted to add that there are some flaws in the results given that I cant put my finger on the source. For example 'wrong' set to return all 3 letter permutations that match the 56,000 word dictionary omits the obvious 'now'.

Any ideas as to why?
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Short script to help solve word puzzle game

Posted 09 July 2020 - 06:58 AM

Is there anything odd about the word 'wrist' in your text file, compared to the other words and lines? E.g. does it have any unusual (or hidden) characters around it?
And everything is lowercase?
Was This Post Helpful? 1
  • +
  • -

#4 DB9323   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 18-November 17

Re: Short script to help solve word puzzle game

Posted 09 July 2020 - 07:20 AM

Hi,

Thanks for your reply!!

I actually figured out that error. To get all the four letter combinations, you need to specify a length of 5. Due to python list beginning at zero, :)

I would still like to make this more effficient - and also to make it so that the user has only to specify the maximum length of matching words and then for python to generate all combinations of ay length up to and including the maxium specified length.
Was This Post Helpful? 1
  • +
  • -

#5 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,847
  • Joined: 27-December 13

Re: Short script to help solve word puzzle game

Posted 09 July 2020 - 08:52 AM

I can see form post #4 that you have updated the code. My reply, however, refer to the code in post #1.

In line 40-41 you constantly overwrite the previous result. If you are only interested in a result of fixed length, you can simply do:
possible_permutations = ([''.join(p) for p in permutations(character_string, result_length)])


Line 48-52 is not necessary (in fact they are harming by including the start-word an extra time) - just delete.

Line 11 is def load_dictionary(filename): - but you never use this 'filename' argument.

The function in line 28-30 is never used.
Was This Post Helpful? 1
  • +
  • -

#6 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,847
  • Joined: 27-December 13

Re: Short script to help solve word puzzle game

Posted 09 July 2020 - 09:00 AM

More:
The bulk of your processing time will be in the function "get_good_words()".
This function might benefit from the set operator 'intersection' - it goes like this:
def get_good_words(possible_permutations):
    return set(ALL_WORDS).intersection(set(possible_permutations))


Short and sweet :innocent:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1