Python - finding intersections

Asked by At

I am trying to have the below code return True if the letters in secretWord are also in lettersGuessed. Could someone please let me know what is wrong with my code? It is consistently returning False. Thanks so much.

def isWordGuessed(secretWord, lettersGuessed):
    current = ' '
    for c in secretWord:
        if c in lettersGuessed:
            current += c
            if len(current) == len(secretWord):
                return True
            else:
                return False
print(isWordGuessed(secretWord, lettersGuessed))

5 Answers

0
stackErr On Best Solutions

Iterate over each letter in the secret word and check if its in the second word. Store all the common letters in a out. Check the length of out and return True if greater than 0.

def isWordGuessed(s1, s2):
  out = ""

  for c in s1:
    if c in s2 and not c in out:
      out += c

  if len(out) > 0:
    return True

  return False

Also it looks like your function is doing too many things. Do you also want to check that the length of the guessed word and the length of secret word is the same? What happens if the secret word or guessed word has repeating letters?

1
Josep Valls On

You are checking if len(current) == len(secretWord) inside of the loop, before you have completed checking the letters in secretWord. Move it outside. Otherwise, you may want to address this problem using sets as @stackErr suggested.

0
Perennial On

Your current variable already has a "space" in it. So You will always have an extra count in your length. Secondly, whey not use current_length = 0 instead and change the line 5 to current += 1. Then you can compare the len() with this count directly instead of calling len for each word. Since your code does not consider duplicates, you can write a much more compact function using sets.

guess = set(lettersGuessed)
secret = set(secretWord)
if secret.intersection(guess) == secret:
    return True
else:
    return False

>>> a = set("abcdefgh")
>>> b = set("efghabcd")
>>> a.intersection(b)
set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'h'])
>>> a.intersection(b) == a
True

Note that this does not consider duplicate characters etc. For that you should consider using a dictionary and keep track of counts of each character.

0
Meena Nagarajan On

There are two issues here. One is to get the code working as is. Remove the additional space between ''. Now, the if len(..) statement should be indented below the 'for' statement since we want all the letters in the secretword to be checked i.e the entire for loop needs to finish before checking the length.

def isWordGuessed(secretWord, lettersGuessed):
    current = ''
    for c in secretWord:
        if c in lettersGuessed:
            current += c
    if len(current) == len(secretWord):
        return True
    else:
        return False

The other issue to address is what is required from the code. Is it supposed to return True only when all letters in secretWord appear in lettersGuessed. For example, 'newd' and 'ne4wd'. Are repetitions allowed? Just something to keep in mind.

0
Bikramjeet Singh On

You can compare the lengths of secretWord and current inside the for loop but once comparison is successful you need to break out of it to then compare if both are indeed the same strings:

def isWordGuessed(secretWord, lettersGuessed):

    current = ''

    for i in secretWord:
        if i in lettersGuessed:
            current = current + i
            if len(secretWord) == len(current):
                break

    if secretWord == current:
        return True
    return False