1

Write a function isAllLettersUsed(word, required) that takes in a word as the first argument and returns True if the word contains all the letters found in the second argument.

Examples

>>> isAllLettersUsed('apple', 'apple')
True
>>> isAllLettersUsed('apple', 'google')
False
>>> isAllLettersUsed('learning python', 'google')
True
>>> isAllLettersUsed('learning python', 'apple')
True

What i'm Doing is

def isAllLettersUsed(word, required):
    if all(required in word for required in word):
        return True
    else: 
        return False

And Result returning is

True
True
True
True

Where as it should return as

True
False
True
True

i don't understand what should i do at this point i have tried many things but failed. any suggestion??

1
  • for required in word means "for each letter in word, assign it to a variable named required (and incidentally shadow the old one)." You probably want to use for ch in required or something similar. Commented Jul 28, 2015 at 18:39

3 Answers 3

5

Just see if all letters in required are in word:

def  isAllLettersUsed(word, required):
    return all(ch in word for ch in required)

You are checking if the each letter from word is in word by using required in your for loop, required refers to each character not the required parameter passed so it always returns True as every letter from word has to be in the word.

Sign up to request clarification or add additional context in comments.

1 Comment

No worries, you could use sets which for large strings would be more efficient but I think that is getting away from the main point of your question
1
def isAllLettersUsed(word, required):
    if all(req in word for req in required):
        return True
    else: 
        return False

What happened was that you were assigning a value to the variable required in your for loop but required is a parameter. You want to loop through required and save each element of required to a new variable. Then, check if that element is in word.

Additionally, your if-else is redundant if you use all(). Just say return all(...)

Comments

0

Change the name of your variables in your generator expression. You shadowed your argument named required with your loop variable required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.