-4

Lets say I have a list letters = ["a", "c", "t"], how would I check if those letters could make the word "cat"?

Hugh Chalmers
  • 49
  • 1
  • 9

2 Answers2

0

Not sure why your question is drawing so much bad attention. permutations are what you need:

from itertools import permutations

def is_perm(letters,word):
    for p in permutations(letters):
        if ''.join(p) == word: return True
    return False
letters = ["a", "c", "t"]
word = 'cat'
print is_perm(letters,word)

Letters may of course be any list of strings, not just letters.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • It used to be a different question, but I changed that. However since then it's still been going down. Not sure why, too. Thanks for the help tho. :) – Hugh Chalmers Nov 16 '16 at 21:12
  • 2
    I did not personally downvote, but I believe others may have done so because you did not show that you attempted anything before asking the question. – Elliot Roberts Nov 16 '16 at 21:18
  • + Elliot Roberts Makes sense I guess. – Hugh Chalmers Nov 16 '16 at 21:20
  • People should really comment when they downvote, it's polite and helpful. Otherwise you're missing the point of stackexchange, helping (IMO). – kabanus Nov 16 '16 at 21:21
  • 1
    @kabanus people also tend to downvote answers to lazy questions, to discourage users from answering them. I guess this is what happened here. – vaultah Nov 16 '16 at 21:28
  • @HughChalmers if this answers your question you should mark it accepted with the check mark to the left `<------` – Wayne Werner Dec 01 '16 at 14:01
0

This would do the job. It checks to see if all of the letters are in the word, and all of the word letters are in the list.

letters = ['c', 'a', 't']
word = "cat"

def check_word(lst, word):
    for letter in lst:
        if letter not in word:
            return False # "this series does not work"

    for letter in list(word):
        if letter not in lst:
            return False # "this series does not work"


    return True # "this works"

check_word(letters, word) # returns True
Jake Conway
  • 901
  • 16
  • 25