1

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
sim
  • 524
  • 3
  • 14

3 Answers3

2

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()))
0x5453
  • 12,753
  • 1
  • 32
  • 61
  • can you check below test `allowed_words = set(['ABC','CDE','EFG']) target_string = 'EFG EFG cde' print(all(word.lower() in allowed_words for word in target_string.lower().split()))` I am getting False instead of True – sim Jul 07 '22 at 13:35
  • 1
    You would need to apply the `.lower()` transformation to `allowed_words` as well. But since those words are already all uppercase, you could just swap `.lower()` for `.upper()` in your example. – 0x5453 Jul 07 '22 at 13:46
1

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 candidate.split(" "))

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

2e0byo
  • 5,305
  • 1
  • 6
  • 26
0

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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • can you check below test allowed_words = set(['ABC','CDE','EFG']) target_string = 'EFG EFG cde' print(all(word.lower() in allowed_words for word in target_string.lower().split())) I am getting False instead of True – sim Jul 07 '22 at 13:35
  • Not sure why you commented here as I propose this solution, but it would make more sense to be: `print(all(word.upper() in allowed_words for word in target_string.split()))` – Tomerikoo Jul 07 '22 at 15:39