Python | Print all string combinations from given numbers



Examples :

  Input:  191  Output:  aia sa  Explanation:  The Possible String digit are 1, 9 and 1 - & gt; aia 19 and 1 - & gt; sa  Input:  1119  Output:  aaai aas aki kai ks 

Suitable :

  • Get a string and find the entire list of combinations in the specified order.
  • Find a list whose integers are in the range 0 to 25.
  • Convert integers to alphabets.
  • Sort this in lexicographic order.

# Python program to print the entire line
# combination of the given numbers

 
# Function for finding a combination

def Number_to_String (String):

 

# line length

  length = len (String)

 

  # temporary lists

temp1 = []

temp2 = []

 

  # alphabet sequence

alphabet = "abcdefghijklmnopqrstuvwxyz"

 

# Variable power

  power = 2 * * (length - 1 )

 

for i in range ( 0 , power):

 

# timeline

sub = " "

Shift =

x = 0

sub + = String [x]

x + = 1

for j in range (length - 1 ):

if Shift & amp; 1 :

sub + = ""

Shift = Shift & gt; & gt; 1

sub + = String [x]

  x + = 1  

temp1.append ( list ( map ( int , sub.split ())))

 

# Integer to string

for index in temp1: 

substring = ""

for j in index:

if j & gt;  0 and j & lt; = 26

  substring + = alphabet [j - 1 ]

if len (substring) = = len (index):

temp2.append (substring)

 

# lexicographic order sorting  

print ( * sorted (temp2), sep = "" )

 
Driver code

Number_to_String ( "191" )

Number_to_String ( "1991" )

Number_to_String ( "1532" )

Number_to_String ( "1191" )

Output:

 aia sa aiia sia aecb ocb aaia asa kia