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?

    Hello,

    I am writing a "scrabble code" on Python. I have some problem with getting possible words from the given letters. For example:
    Given letters are: 'c', 'r', 'n', '*'
    Here '*' can be any letter.
    Possible words: 'cry', 'corn', 'run', etc.
    proper_word_lis t is a file containing all the english words.
    score_of_word is a function that computes the score of the word.
    hand is the letters that are given to the player

    Following is my code (it gets only the possible words without star in hand):

    Code:
    def get_possible_words(hand, proper_word_list, score_of_a_letter_list) :
        
        possible_words = []
        for j in range(len(proper_word_list)):
            k = list(proper_word_list[j])
            k.sort()
            c = "".join(k)
            d = list(hand)
            d.sort()
            m = "".join(d)
            if c in m:
                l = (score_of_word(c, score_of_a_letter_list), proper_word_list[j])
                possible_words.append(l)
        possible_words.sort()
        possible_words.reverse()
        return possible_words

    Thank you in advance.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I take it this is homework? I would approach it this way:
    1. Save the given letters ['c', 'r', 'n', '*'] - listA
    2. Create another list, leaving out the asterisk ['c', 'r', 'n'] - listB
    3. Open the file, creating a file object
    4. Initialize a list to contain the matching words - output
    5. Iterate on the file object, using variable line
    6. Strip the newline character
    7. If the length of the stripped line is in between the lengths of listA and listB
    8. and if each letter in listB is in line, append to output


    See if this helps. I think calculating the word score would be straightforward , but post again if you have any more questions.

    Comment

    • Jimmy45
      New Member
      • Apr 2010
      • 8

      #3
      Originally posted by bvdet
      I take it this is homework? I would approach it this way:
      1. Save the given letters ['c', 'r', 'n', '*'] - listA
      2. Create another list, leaving out the asterisk ['c', 'r', 'n'] - listB
      3. Open the file, creating a file object
      4. Initialize a list to contain the matching words - output
      5. Iterate on the file object, using variable line
      6. Strip the newline character
      7. If the length of the stripped line is in between the lengths of listA and listB
      8. and if each letter in listB is in line, append to output


      See if this helps. I think calculating the word score would be straightforward , but post again if you have any more questions.
      Thank you for your kind attention to my question. Yes, I am doing my python homework. I have a question about how to check if every element in one list is contained in the another list. For example:
      a = ['a', 'b', 'c'] and b = ['c', 'a']

      Comment

      • woooee
        New Member
        • Mar 2008
        • 43

        #4
        Sets do that
        Code:
        a = set(['a', 'b', 'c'])
        b = set(['c', 'a'])
        print b.issubset(a)

        Comment

        • Jimmy45
          New Member
          • Apr 2010
          • 8

          #5
          Originally posted by woooee
          Sets do that
          Code:
          a = set(['a', 'b', 'c'])
          b = set(['c', 'a'])
          print b.issubset(a)
          Is there any other way to do it? Because we can't use any set command in our homework.


          Thank you.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Maybe this will help:
            Code:
            >>> listA = ['a','b','c']
            >>> word = 'abacus'
            >>> [(c in word) for c in listA]
            [True, True, True]
            >>> None in [(c in word) for c in listA]
            False
            >>>

            Comment

            • Jimmy45
              New Member
              • Apr 2010
              • 8

              #7
              Hello!

              I am doing my homework and trying to get possible words from given letters.
              For example:
              Here is your hand: AONEP

              Enter your word: ?
              (POPPA, 11)
              (NAPPE, 9)
              (NAPPA, 9)
              (POPE, 8)
              (POOP, 8)
              (PEPO, 8)
              (PEEP, 8)
              (PAPA, 8)

              It must use p only once, but here is more that one. Can you please help me to find my mistake. Following is my code:

              proper_word_lis t is given and it is converted to list in the another function.
              hand is the letters given to the player.
              Code:
               def get_possible_words(hand, proper_word_list, score_of_a_letter_list) :
                  
                  possible_words = []
                  for j in range(len(proper_word_list)):
                      if len(proper_word_list[j]) <= len(hand):
                          str1 = ""
                          for letter in proper_word_list[j] :
                              if letter in hand:
                                  str1 = str1 + letter 
                              if '*' in hand:
                                  num_of_l = len(str1)
                                  num_of_ast = hand.count('*')
                                  num_dif = len(proper_word_list[j]) - num_of_l
                                  if num_of_ast >= num_dif:
                                      str1 = proper_word_list[j]
                              else:
                                  str1
                      if str1 == proper_word_list[j]:
                          l = (score_of_word(str1, score_of_a_letter_list), str1)
                          possible_words.append(l)
                  possible_words.sort()
                  possible_words.reverse()
                  return possible_words
              Thank you in advance.

              Comment

              Working...