0

I'm trying to build a program in Python that, given a list of words and a list of letters, can determine what words can be made with 7 random letters from the list of letters.

I have code that shows all possible words from the list that are possible with all the letters in the list, but I need to change it to choose 7 random letters from the list, then see which words from the list are possible to make with those chosen letters.

Here's the code that uses all letters and determines what words are possible with them:

from collections import Counter

words = ['hi','how','are','you']
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b'] 

# For every word
for word in words: 

    # Convert the word into a dictionary
    dict = Counter(word) 
    flag = 1
    
    # For every letter in that word
    for key in dict.keys():

        # If that letter is not in the list of available letters, set the flag to 0
        if key not in letters: 
            flag = 0

    # If the flag remains 1 (meaning all letters in the word are in the letters list), then print the word.
    if flag == 1:
        print(word)

Right now, it correctly prints the possible words with all the letters, but I want it to choose 7 random letters from the list and print what words are possible.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Rohan
  • 132
  • 4
  • 21
  • Question, can the list `['h', 'a', 'p', 'y']` make the word `'happy'`? – Mike May 28 '19 at 23:07
  • No, it can't. It's supposed to be similar to Scrabble. – Rohan May 28 '19 at 23:09
  • With the code I have, it does, even though it shouldn't be able to. I don't know how to fix this... – Rohan May 28 '19 at 23:10
  • Which part are you having trouble with? Choosing seven random letters from the list? – wwii May 28 '19 at 23:11
  • Yes, and using those numbers only in the rest of the program. – Rohan May 28 '19 at 23:11
  • To make `'happy'` unplayable from `['h', 'a', 'p', 'y']`, the condition you're after is `letter in letters.keys() and word_counter[letter] <= letters[letter]` for every letter in the word_counter (don't call it dict, as it shadows the python built-in name) – Mike May 28 '19 at 23:23

2 Answers2

1

Random.sample():

import random as rnd
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b'] 
hand = rnd.sample(letters, 7)

Then proceed as before.

bkyada
  • 332
  • 1
  • 9
Mike
  • 828
  • 8
  • 21
  • 1
    don't you mean `rnd.sample` not `rnd.select` – Jab May 28 '19 at 23:15
  • 1
    Apparently rnd.select() is not a thing - it says "module random has no attribute select." – Rohan May 28 '19 at 23:16
  • 1
    Ah I fixed it by using rnd.sample. Thanks Jab! – Rohan May 28 '19 at 23:16
  • This works! Is there a way to make it so that if the letters it chose include 'h', 'a', 'p', and 'y', then the word 'happy' can't be spelled? Right now it ignores whether there's the right number of a letter in the letters list. – Rohan May 28 '19 at 23:18
  • See my comment on your OP, @RohanTaneja! – Mike May 28 '19 at 23:18
0

You can use Counter from collection to check for the ability to make each word out of the letters:

words   = ['hi','how','are','you']
letters = ['h','i','b', 'a','r','e', 'l', 'y', 'o', 'u', 'x', 'b']

from random      import sample
from collections import Counter

letterCounts  = Counter(sevenLetters)
wordCounts    = { word:Counter(word) for word in words }

sevenLetters  = sample(letters,7)
possibleWords = [ word for word,wc in wordCounts.items() if not wc-letterCounts ]

print(sevenLetters,possibleWords) 
# ['x', 'u', 'h', 'b', 'a', 'i', 'b'] ['hi']

Note: using Counter() covers the cases where the same letter is needed more than once (ex:baby).

Alain T.
  • 40,517
  • 4
  • 31
  • 51