How to get possible words from the given letters?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jimmy45
    New Member
    • Apr 2010
    • 8

    How to get possible words from the given letters?

    I have some problem with one task (only this task left from 12 tasks). I am trying to write a scrabble code on python.

    I have to write a function that gets all the possible words from the given letters in hand. And there is an asterisk that can be replaced by any letter.
    For example:

    Given letters = 'crn*'
    Words can be = 'cry', 'corn' and etc. I can't get all the possible words when there is an asterisk.

    Can you give me some hints for this problem?
  • woooee
    New Member
    • Mar 2008
    • 43

    #2
    You want to first count the number of asterisks; that will be the cut-off. Next, compare the given word to all possibles. To allow for two letters that are the same, say a word has two "o"s for example, sort both words and compare with a loop, comparing each letter in turn. Increment through the word if a letter is found.

    You can also use lists, which is probably easier. Use the first word as the control. Convert the second word to a list. Iterate through the first word letter by letter and if the letter is found in the list, delete it, otherwise add one to a misses variable. Note that either word can be the longer so you have to allow for both cases.

    Of course, it the length of the words is off by more than the number of asterisks it is an automatic fail.

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      Presumably (from the previous posts) you've solved the problem of doing this without any asterisk. So why not just have loops going from a-z replacing each asterisk with one letter, and checking them all.

      Of course, this is terribly inefficient to *run*, but might be efficient to code!

      Itertools is your friend for this kind of thing.

      Otherwise, write a function that tells you how much overlap there is between a test word, and the letters in your list, and how many letters are left over. If the number of letters left over is less than the number of asterisks then you can make the word.

      It's this the exercise where you're not allowed to use set functions? Because that would make your life easy!

      Comment

      Working...