Function that determines whether all letters contained in one string are in the other

Asked

Viewed 106 times

0

def isWordGuessed(secretWord, 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
    '''
    certas =0
    for i in range(len(secretWord)):
        if secretWord[i] in lettersGuessed:
            certas += 1

    #print(certas)

    if certas == len(secretWord):
        return True


    else:
        return False

secretWord = 'durian' 
#lettersGuessed = ['e', 'i', 'k', 'p', 'r', 's']
#lettersGuessed = ['e', 'a', 'l', 'p', 'e']
#isWordGuessed('durian', ['h', 'a', 'c', 'd', 'i', 'm', 'n', 'r', 't', 'u'])
lettersGuessed = ['h', 'a', 'c', 'd', 'i', 'm', 'n', 'r', 't', 'u']
print(isWordGuessed(secretWord, lettersGuessed))

Purpose is to determine whether all letters of secretWord are contained in lettersGuessed.

My code is working. Is there any Pythonic way to do?

2 answers

3


You can do it in a row:

def is_word_guessed(secret_word, letters_guessed):
     return all([letter in letters_guessed for letter in secret_word])

print(is_word_guessed('abcd',  ['a', 'b', 'd', 'c']))  # True

print(is_word_guessed('abcd',  ['a', 'b', 'e', 'c']))  # False

The function all returns True if all values in the list passed to it are True, and False if one or more of the values is False.

List comprehension analyzes each letter in secret_word and adds a True element if the parsed letter is in letters_guessed and False if not.

  • all is a function?

  • 1

    Yes. It doesn’t even have to matter. It’s worth knowing all and her sister any.

  • 3

    You can remove the clasps inside all to, instead of creating a list, create a generator. So the interpreter will only need to evaluate until it finds the first false, which can save time and application memory.

2

Another way of doing:

def isWordGuessed(secretWord, lettersGuessed):
    return set(secretWord).issubset(set(lettersGuessed))

The advantage I see in this case is to save the verification of repeated characters.

Take for example:

secretWord = 'aaaabcdddefg'
lettersGuessed = 'abcdefg'

When using all(), is traveling secretWord 12 times and lettersGuessed up to 7 times for each of these 12. (This is a simple case of few characters and where the strings are sorted).

  • And to generate one set Aren’t all characters covered? The way you put it, it would be like set not being O(n).

  • @Andersoncarloswoss, in fact. Only it’s better to have possible 12+7 iterations of what 12*7 (worst case), don’t you think? Also remember that the average execution time of an intersection between sets is: O(min(len(s), len(t))

  • Yes, I believe so. I need to read to be sure, but I think you’re right about that. I had answered this way too, but ended up not publishing by wishing to analyze better and ended up forgetting.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.