0

I have a list of letters.

I need to define a function that will take two arguments, first is a string, second is a list of letters. Then return a bool indicating whether all letters in the string are in the list of letters or not.

I tried for loops but it would only check the 0 index and nothing else.

lettersGuessed = ['a', 'b','c', 'p', 'l', 'e']

def isWordGuessed(str, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    if all(list(str)) in lettersGuessed:
        return True
    else:
        return False
print(isWordGuessed('bcp', lettersGuessed))
Zidane M.
  • 13
  • 4
  • https://stackoverflow.com/questions/16579085/python-verifying-if-one-list-is-a-subset-of-the-other – AcaNg Jul 27 '19 at 18:58
  • Do letter counts matter? `set('reed').issubset('cred')` evaluates to `True`. – Steven Rumbalski Jul 27 '19 at 18:59
  • @DeveshKumarSingh can you reopen the question? Using subset is not applicable here since for example if the `secret_word` is `apple` and the `letters_guessed` are `aple` that should return `False`. I have an answer that deals with that case I would like to post for OP. – Sash Sinha Jul 27 '19 at 19:23
  • @shash678 but in the OP's case, `lettersGuessed` is a list of letters, not a list of words. – AcaNg Jul 27 '19 at 19:33
  • I have added another target. Also the op clearly says `Then return a bool indicating whether all letters in the string are in the list of letters or not.` which either `issubset` or `all` will do @shash678 – Devesh Kumar Singh Jul 27 '19 at 19:43
  • @Tiendung I meant ['a', 'p', 'p', 'l'] sorry. – Sash Sinha Jul 27 '19 at 21:42
  • @DeveshKumarSingh neither `all` or `issubset` take into account the frequency of the letters in `lettersGuessed` in comparison to the secret word. Whatever... I just thought a function called `isWordGuessed` should not return true for a secret word of `apple` and `letter_guessed` of `['a', 'p', 'l', 'e']`. – Sash Sinha Jul 27 '19 at 21:47

2 Answers2

0

You answer is pretty simple

def isWordGuessed(str, lettersGuessed):
    return all(letter in lettersGuessed for letter in str)

Also please dont use built-in names like str as variables

Saritus
  • 918
  • 7
  • 15
0

I don't recommend either of these, but the simple solution is to use a for loop:

for c in str:
    if c not in lettersGuessed:
        return False
return True

or a comprehension:

return all(c in lettersGuessed for c in str)

But you shouldn't do it this way. Lists are a fundamentally bad data structure for checking if an element is present. Use a set instead:

return all(c in lettersGuessed for c in set(str))

Ideally you would make str a set from the start. See the docs for some examples.

Lastly, don't call str "str". "str" is a variable used by Python itself. When you name something else str you are overwriting that variable. Use "string" or "real_string" or something like that instead.

vluzko
  • 335
  • 1
  • 8