Home > Net >  How to check if a string is only made-up from a list of words?
How to check if a string is only made-up from a list of words?

Time:07-08

I have a list of words:

words = ['ABC', 'CDE', 'EFG']

How to check that my string only consists words from that list? For example, 'EFG CDE' results True since both 'CDE' and 'EFG' are in words.

My code is below:

lmn = []
for j in list(itertools.permutations(words, 2))   list(itertools.permutations(words, 3)):
    lmn.append(' '.join(j))

'EFG CDE' in lmn

My output is giving True which is correct.

But for strings like 'EFG EFG CDE', 'CDE CDE CDE CDE' it will not give True because these strings are not present in lmn. Even if they are made of the list ['ABC', 'CDE', 'EFG'] only.

CodePudding user response:

Here's how I would do it:

allowed_words = set(['ABC','CDE','EFG'])
target_string = 'EFG EFG CDE'
print(all(word in allowed_words for word in target_string.split()))

CodePudding user response:

Rather than trying to build every possible permutation and then checking (which will be unbounded if the input is unbounded), just do the search yourself.

The problem is 'check every component part of the string is present in an iterable' where component part is defined as 'part separated by a space':

def check_string_made_of_parts(candidate, parts):
    return all(part in parts for part in candate.split(" "))

With these kind of problems in python it's helpful to talk through a sensible algorithm in words before you hit any code.

CodePudding user response:

If you break down your problem, you have a list (could be a set?) of allowed words and a string made up of words.

You want to check that every word in your string is in the list of allowed words. Translated loosely, you want to check that the set of words in your string is a subset of the set of allowed words.

Now there are two steps:

  1. Get the set of words from the string.
  2. Check if it is a subset of the allowed words.

(1) is quite easy - words = set(s.split())

(2) can be done by using basic set operations:

words.issubset(allowed_words)  # allowed_words doesn't need to be a set
words <= set(allowed_words)
  • Related