Hello,

I am trying to figure out how to search determine if a word has all the letters in a predetermined set. So for example, I give the letters "yx" and I search a database of words and pull the words that contain only those letters. So if I used the previous letters a word like "x-ray" would show up. This is what I have so far:

all((c in chars) for c in word)

If I use any instead of all I can get all words just 1 of letters. But why can't I do it this way?!?!!

Cheers,

First, what language are you using? Next, why isn't this in the Software Development forum?

hmmm I just noticed that. Sorry. Thought i out it under Python....

So you mean you want a function that determines if a word contains all of the specified letters; ie: has_all_letters("x-ray", "xy") would return true.

Start by iteration through all of the characters in the "xy" string. For each character. Check if the character exists within the "x-ray" string. If a letter does not exist, return false. If the end if the loop is reached, return true.

That paragraph should pretty much corralete one-to-one to the final code.

Thanks. Got it working it your way. If there is any more "pythonic" way of coding it please let me know.

Cheers,
cg

I'm not sure of a more Pythonic way of doing it, but there are faster (a little more complicated though) ways of doing it.

Right now it's running at O(m*n) time (m is the size of the "x-ray" string, n is the size of the the "xy" string). You can get this down to O(m+n) time by storing all of the letters in the "x-ray" string in a set, storing all of the letters in the "xz" string in another set, and confirming that the intercept is the same size of the "xy" string itself. There are a few other ways of getting the O(m+n) time though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.