Possible Words using given characters in Python

PythonServer Side ProgrammingProgramming

In this article we are going to see a python program that will give output of possible words from a given set of characters. Here we are taking a list as an input which will contain the set of reference words and another list containing the characters from which the words are made up of.

In the below program, we define two functions. One to take the letters from the second list and make up words. Another function to match the words formed with the words present in the given list of words.

Example

 Live Demo

def Possible_Words(character):
   x = {}
   for n in character:
      x[n] = x.get(n, 0) + 1
   return x
def character_set(w, character):
   for char in w:
      value = 1
      m = Possible_Words(char)
      for k in m:
         if k not in character:
            value = 0
         else:
            if character.count(k) != m[k]:
               value = 0
      if value == 1:
         print(char)
data = ['fat','tap','day','fun','man','ant','bag','aim']
words = ['m','t','e','d','f','a','p','y','i']
character_set(data, words)

Output

Running the above code gives us the following result −

fat
tap
day
aim
raja
Published on 03-Mar-2020 10:04:28
Advertisements